Article

Rails For Beginners: All You Need To Know!

Page: 1 2 3 4 5 6 Next

Using the Rails Console

Now that we have our stories table in place, let's exit the SQLite console (simply type .quit) and open up a Rails console. A Rails console is just like the interactive Ruby console (irb) that we used in Chapter 3, but with one key difference. In a Rails console, you have access to all the environment variables and classes that are available to your application while it's running. These are not available from within a standard irb console.

To enter a Rails console, change to your shovell folder, and enter the command ruby script/console, as shown below. The >> prompt is ready to accept your commands:

$ cd shovell  
$ ruby script/console  
Loading development environment (Rails 2.0.2)  
>>

Saving an Object

To start using ActiveRecord, simply define a class that inherits from the ActiveRecord::Base class. (We touched on the :: operator very briefly in Chapter 3, where we mentioned that it was a way to invoke class methods on an object. It can also be used to refer to classes that exist within a module, which is what we're doing here.) Flip back to the section on object oriented programming (OOP) in Chapter 3 if you need a refresher on inheritance.

Consider the following code snippet:

class Story < ActiveRecord::Base  
end

These two lines of code define a seemingly empty class called Story. However, this class is far from empty, as we'll soon see.

From the Rails console, let's create this Story class, and an instance of the class called story, by entering these commands:

>> class Story < ActiveRecord::Base; end  
=> nil  
>> story = Story.new  
=> #<Story id: nil, name: nil, url: nil, created_at: nil,  
      updated_at: nil>  
>> story.class  
=> Story(id: integer, name: string, link: string,  
      created_at: datetime, updated_at: datetime)

As you can see, the syntax for creating a new ActiveRecord object is identical to the syntax we used to create other Ruby objects in Chapter 3. At this point, we've created a new Story object. However, this object exists in memory only -- we haven't stored it in our database yet.

We can confirm the fact that our Story object hasn't been saved yet by checking the return value of the new_record? method:

>> story.new_record?  
=> true

Since the object has not been saved yet, it will be lost when we exit the Rails console. To save it to the database, we need to invoke the object's save method:

>> story.save  
=> true

Now that we've saved our object (a return value of true indicates that the save method was successful) our story is no longer a new record. It's even been assigned a unique ID, as shown below:

>> story.new_record?  
=> false  
>> story.id  
=> 1

Defining Relationships Between Objects

As well as the basic functionality that we've just seen, ActiveRecord makes the process of defining relationships (or associations) between objects as easy as it can be. Of course, it's possible with some database servers to define such relationships entirely within the database schema. However, in order to put ActiveRecord through its paces, let's look at the way it defines these relationships within Rails.

Object relationships can be defined in a variety of ways; the main difference between these relationships is the number of records that are specified in the relationship. The primary types of database associations are:

  • one-to-one associations
  • one-to-many associations
  • many-to-many associations

Let's look at some examples of each of these associations. Feel free to type them into the Rails console if you like, for the sake of practice. Remember that your class definitions won't be saved, though -- I'll show you how to define associations in a file later.

Suppose our application has the following associations:

An Author can have one Weblog:
 
class Author < ActiveRecord::Base  
 has_one :weblog  
end  

An Author can submit many Stories:
 
class Author < ActiveRecord::Base  
 has_many :stories  
end  

A Story belongs to an Author:
 
class Story < ActiveRecord::Base  
 belongs_to :author  
end  

A Story has, and belongs to, many different Topics:
 
class Story < ActiveRecord::Base  
 has_and_belongs_to_many :topics  
end  
class Topic < ActiveRecord::Base  
 has_and_belongs_to_many :stories  
end  

You're no doubt growing tired of typing class definitions into a console, only to have them disappear the moment you exit the console. For this reason, we won't go any further with the associations between our objects -- we'll delve into the Rails ActiveRecord module in more detail in Chapter 5.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links