Loading data from joined table

Hi,

I working on a rating system for the posts on my blog:
My tables are
posts

  • id
  • title
  • date

and

ratings

  • id
  • post_id
  • rating

If have these models
class Rating < ActiveRecord::Base
belongs_to :post
end

and

class Post < ActiveRecord::Base

has_one :rating

end

Now in my controller and want to load also the rating for a post:
Controller:
def index
@post_pages, @posts = paginate(:posts, :order => ‘date DESC’)
end

Index.rhtml
<% for post in @posts %>
<%= post.title %>
<%= post.post %>
<%= post.rating.rating %>

How can I get the last line to work?

Thanks,
MJ

Hi,
The kind of relation you have within tables,

try
Rating.find(post.id)

regards
gaurav

Hi,

I misunderstood your question my previous reply not relevant
I apologise

regards
gaurav

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs