Practical eCommerce

 

Relationships in Ruby on Rails

avatar

I figured I would post about creating relationships between database tables because I have been learning more and more about Ruby on Rails, and following some tutorials to learn more. As I have posted in the past, I was immediately impressed with how little coding was required to get a lot of work done. Specifically when it comes to a database-driven web application, where writing configuration files and scripting the database connections and queries is super tedious, and not really my cup of tea. Don’t forget, my background is in graphic design and web design, not computer science.

So one thing that I came across while learning more about Rails is that to create relationships between data in the database tables is almost freakishly easy. At least when compared to what it takes to put in the same functionality using PHP, at least at my level of programming. For example, let’s say that you have some business rules that need to apply for your application. For example, we have articles on our website that are assigned to categories.

In the first example, our business rule is: “Each article can be assigned to only one category.” In a Rails application, we would have a model for articles (Article) and a model for categories (Category). To define the relationship in Rails, we would open the categories model file (app/models/category.rb) and add the bold code below:

class Category < ActiveRecord::Base  
  has_many :articles  
end

This tells Rails that each category can have many articles in it. Seriously, that’s all it took. Next we want to open up the model file for the articles (app/models/articles.rb) and tell Rails that each article can only go in one category:

class Articles < ActiveRecord::Base  
  belongs_to :category  
end

It’s that simple. What does that do? Well, let’s say that you have created a categories, and each one has a few articles assigned to them. If you were to try and delete a category, logic tells us that we have a problem. And now so does Rails. If you try and delete a category that has articles assigned to it, Rails will generate and error letting you know that. With 2 lines of code!!!

Of course, we would eventually need to code in some decision making as to what to do when someone tries to delete a category that has articles assigned to it, but that is another post. Right now I am just marveling (and sharing) in how little code it takes in Rails to do what would take me all day to do with PHP and MySQL. To give an idea of other options, such as if we wanted to be able to assigne articles to many categories, which would then create a many-to-many relationship. The business rule would be “Each article can be assigned to many categories, and each category can have many articles. In this case we would modify both model files to reflect the following:

(app/models/category.rb):

class Category < ActiveRecord::Base  
  has_and_belongs_to_many :articles  
end

(app/models/articles.rb):

class Articles < ActiveRecord::Base  
  has_and_belongs_to_many :category  
end

As you can see, that was pretty painless. We have then told our Rails application that it needs to be aware of these relationships between tables in the database, and to act accordingly. All that as opposed to coding the logic ourselves. These can even be combined to give you something like this for the articles model:

(app/models/articles.rb):

class Articles < ActiveRecord::Base  
  belongs_to :author  
  has_and_belongs_to_many :category  
end

Translating the relationships into english and you get the following business logic: “Articles can belong to one author, but can be assigned to many categories”. I’m sure this is getting repetitive and boring, and I’m not expert on this subject. In fact, I’m posting this to proclaim how surprised I was when I finally absorbed this, and how easy this was. Writing a few sample applications and I have found that it would take me days to write the code required (mostly because I’d have to learn it), and I probably wouldn’t cover all the bases like Rails does.

Just wait until I get into how easy data validation is.

This post is filed under Developers' Corner and has the following keyword tags: ruby, rails, database, php.

Add a Bookmark: Add 'Relationships in Ruby on Rails' to Del.icio.us Digg 'Relationships in Ruby on Rails' on Digg.com Submit 'Relationships in Ruby on Rails' to reddit.com Blink 'Relationships in Ruby on Rails' Add 'Relationships in Ruby on Rails' to dzone Seed 'Relationships in Ruby on Rails' on Newsvine Add 'Relationships in Ruby on Rails' to Furl Add 'Relationships in Ruby on Rails' to Spurl Add 'Relationships in Ruby on Rails' on simpy.com Add 'Relationships in Ruby on Rails' to fark.com BlogMark 'Relationships in Ruby on Rails' Add 'Relationships in Ruby on Rails' to Yahoo! myweb2 Add 'Relationships in Ruby on Rails' to wists.com Stumble It!

0 Comments

Sign-up to receive EcommerceNotes, our acclaimed email newsletter.

View A Sample | Privacy

Bloggers Wanted

We’re looking for merchants and other ecommerce professionals to share their experiences with our readers. If this interests you, we invite you to contact us.

Inside Practical eCommerce