Use following command from your project folder. (Note: Do not use $irb command as it will not work, although the console shows irb prompt).
$rails console # Start rails console
Use "awesome print" gem for better readability. (Refer documentation of awesome print gem.)
$require "awesome_print"
>>ap Event.all (Event is the object we want to view)
>> ObjectName.where(key1: value1).order(:key2).limit(5)
Create
>> ObjectName.create(key1: value1) #Creates a new record with key1 equals to value1 (Preferably use create with a bang, e.g. create! as it raises error on failure. See note below.)
Delete
>> ObjectName.find(2).destroy #Deletes the record with id 2
>> ObjectName.destroy_all #Deletes all records in the table
>> Event.last.delete or Event.last.destroy #Delete last record for Event record
To see errors, try following: (E.g. Title is required, but not enetered. So validation will fail).
>>event = Event.create(venue: "My Venue", location: "Our Location")
>>event.valid?
>>event.errors.count
>>event.errors.full_messages
=> ["Title can't be blank."] # returns an array.
Search
>> Employee.all # return all instances of Employee model
>> Employee.first #Returns the first record
>> Employee.last #Returns the last record
>> Employee.count #Returns count of records
>> Employee.order(:key) #Returns all records, Ordered by key
>> Employee.limit(10) #Returns the first 10 records
>> Employee.find(3) # employee_id is 3
>> Employee.find(3,4,5) or Employee.find([3,4,5]) # employee_id is 3, 4, 5. Returns an array of records with id's 3, 4 and 5
>> Employee.find_by_name("Jordan") # column name is name. find_by returns only 1 row.
>> Employee.find_by_job_type("Salesperson") # column name is job_type. find_by returns only 1 row.
If you want more than 1 rows, then use following:
>> Employee.where(job_type: "Salesperson") # returns all rows for the criteria
>> Employee.where.not(first_name: "John") # returns rows where first name is not John
>> Employee.where(job_type: "Salesperson").first # returns first row only
>> Employee.where(job_type: "Salesperson").last # returns last row only
>> Employee.where(job_type: "Salesperson").limit(2) # returns 2 rows
https://pragmaticstudio.com/tutorials/rails-console-shortcuts-tips-tricks
Update
>> user = User.last
>> user.admin = true #set admin flag to be true
>> user.save
>> ObjectName.update(key1: value1) #Easy way to update
>> user.update(name: "John Doe")
$ rails console -e production #uses production environment
$ rails console --sandbox #If you wish to test out some code and then rollback when you exit console.
Inside the rails console you have access to the app and helper instances.
With the app method you can access named route helpers, as well as do requests.
>>app.root_path
With the helper method it is possible to access Rails and your application's helpers.
>> helper.time_ago_in_words 30.days.ago
>> reload! # when you make any code change and it needs to get reflected in irb session.
Note:
In general, methods that end in ! indicate that the method will modify the object it's called on. Ruby calls these as "dangerous methods" because they change state that someone else might have a reference to.
In the standard libraries, there are a lot of places you'll see pairs of similarly named methods, one with the ! and one without. The ones without bang are called "safe methods", and they return a copy of the original with changes applied to the copy, while the original remains unchanged. Here's an example with and without the !
foo = "BEST DAY EVER" #assign a string to variable foo. => foo.downcase #call method downcase, this is without any exclamation. "best day ever" #returns the result in downcase, but no change in value of foo. => foo #call the variable foo now. "BEST DAY EVER" #variable is unchanged. => foo.downcase! #call destructive version. => foo #call the variable foo now. "best day ever" #variable has been mutated in place.
bang is also used for methods that raise an exception whereas the method without bang does not raise exception. e.g. save and save! in ActiveRecord. In Rails an exclamation point often means that the method will throw an exception on failure rather than failing silently.
By convention, the names of procedures that always return a boolean value usually end in ``?''. Such procedures are called predicates.
By convention, the names of procedures that store values into previously allocated locations usually end in ``!''. Such procedures are called mutation procedures. By convention, the value returned by a mutation procedure is unspecified.