Part 2 – Progressing Website Poll in Rails

June 7, 2008

It has been slow progress so far. So I have decided to take an alternate approach and work through the Depot Application tutorial in Agile Web Development with Rails, Third Edition .

I have been able to make some progress on building the admin interface for Website Poll.  It took me quite some time to understand the associations functionality. 

The first part in associations of setting it up in the models is straight forward enough.  For example :-

class Rating < ActiveRecord::Base

  belongs_to :website

end

and

class Website < ActiveRecord::Base

  has_many :ratings

end

This sets up the associations in the Model but has not effect on the database i.e. you are not setting any constraints such as Foreign Keys on the database.

The area where I had problems was in using the associations setup. However I was “over thinking” it and in the end it was really simple. For example in the view (Index.html.erb) :-

<% for rating in @ratings %>

<tr>

    <td><%=h rating.website.url %></td>

This is the secret to it where it has “rating.website.url” and because of the associations being set it knows that “url” is associated with the website model. It almost seems too simple to work but it does.