How to use the rails console

I call rails console my playground tool. This is where I test my scripts before I integrate it into my project. Let’s get started using it.

First, you have to create a model which we can play with.

Once again, navigate to your project folder using the command line.

cdwampwwwRailsProject

Then generate a Subjects model or any model you want. Always remember that when you generate a model, you are also generating a database migration file:

rails generate model Subjects

If you decided that you don’t want the Subjects model. Then you can always destroy it:

rails destroy model Subjects

Note that it will destroy anything that the generate has created, not only the model. But also the database migration file for that specific model.

 

 

Why use Rails Console?

My answer is that its easier to write scripts in the rails console compared to writing sql statements in the mysql console. Read on so that you’ll see for yourself what I am talking about.

 

Create

First, make sure that you already know how to edit your migration file which is located under db/migrate of your project folder. Here’s the guide. Make sure to follow that and run the migration that you defined before you continue. Here’s what your database migration file should look like:

   class CreateSubjects < ActiveRecord::Migration
     def self.up
       create_table :subjects do |t|
        
         t.string "name"
         t.integer "position"
         t.boolean "visible", :default => false
         
         t.timestamps
       end
     end
    
     def self.down
       drop_table :subjects
     end
   end

Create a new record in the mysql database. Here’s the current state of the table subjects:

image

Now, let’s create a new record in the Active Record:

s = Subjects.new(:name=>"Biology", :position=>4)

To commit the change in the Active Record into your database:

s.save

To check if its indeed committed the go to your mysql console or phpmyadmin:

image

And by the way, if you don’t know what active record is. My definition is that its just like a copy of your database which you can manipulate just like your database. Here’s the long definition from the rubyonrails.org site.

 

Find

There are many ways to find records in your active record:

  • find – returns a single record. Uses primary key
  • all – return an array of all the records.
  • find_by_fieldname – returns a single record. Uses specified fieldname and a single argument.

For example, if you want to find a subject called Math:

s= Subjects.find_by_name("Math")

Note that I only made up the ‘s’ you can use whatever you want.

 

Update

You must first find the record that you want to update before you can use the update method.

search = Subjects.find(5)

You can only update if it returned something:

search.update_attributes(:visible=>true)

The method which I just showed you automatically commits to your database. And you can update different fields at the same time. Just add a comma if you want to update other fields.

Here’s the long-cut method, which you can use to update single fields:

search = Subjects.find(5)

Then assign a value to the field which you want to update:

search.name = "Bio"

Finally, commit the changes to your database:

search.save

 

Delete

You need to find a record before you can delete it. Then just call the method destroy. Be sure to use the same variable in which you have assigned your result. In this case, it is the variable s.

s.destroy

One thought on “How to use the rails console

  1. Very concise and helpful post for those learning Rails. I think its worth adding that you can delete a record and all its related records using destroy on a record instance (s above). You can also clear all records in a table by calling destroy_all on the model.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s