This recipe will show you how to start the Rails console and inspect your code and database interactively.
RubyMine allows you to run both the Interactive RuBy (IRB) console as well as the Ruby on Rails console. They both run in an interactive window at the bottom of your editor much like the debugger and output windows. Perform the following steps:
On the Tools menu, select Run Rails Console and RubyMine will ask you which environment you would like to load into the console. This lets you inspect/change your debug, test, or production environments directly. The default option gives the development environment.
Select OK and the console will open.
The Rails console prompt
>>
means that it is ready for input from the keyboard.Type
2 + 2
and hit Return. The result should be4
as you can see in the following screenshot. This is just like the IRB console, in that you can type Ruby expressions and they will be evaluated interactively.Let's perform some database lookups using Rails syntax.
The proper syntax for Rails 4.0 is something like the following, which you can type in and see the result:
Species.where(assimilated: true).load
The result will be something like this:
Species Load (0.2ms) SELECT "species".* FROM "species" WHERE "species"."assimilated" = 't' #<ActiveRecord::Relation [#<Species id: 3, name: "Human", identification: 5618, assimilated: true, created_at: "2013-06-03 00:58:57", updated_at: "2013-06-03 00:58:57">]>
We can see that the console returned an array of Species
objects that matched that query. It even showed us the SQL query that was used to get the data directly from the database.
There are many wonderful tutorials and tricks online that we can make use of to learn more about the console, starting with the official Rails Guides: http://guides.rubyonrails.org/.