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.
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.
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:
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:
To check if its indeed committed the go to your mysql console or phpmyadmin:
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.
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:
Note that I only made up the ‘s’ you can use whatever you want.
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:
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:
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.