3

I've decided to build my newest site using Rails 3. This is my first experience with Rails and wanted to get the communities opinion on how to do the following scenario.

I have the following models created: Item, Rating, User

I would like the app to work as:

1) Item has many Ratings
2) User can submit many Ratings - Only one user submitted rating per item
3) Specific rating can can only have one Item and one User

Based on this I want to be able to:

1) Show all ratings for an item
2) Show all items rated by a particular user

Seems simple enough. Any help or direction is appreciated greatly.

Scott
  • 65
  • 1
  • 6

1 Answers1

0

I would use Polymorphic association:

# file: app/models/item.rb
class Item < ActiveRecord::Base
  has_many :ratings, :as => :rateable
end

# file: app/models/user.rb
class User < ActiveRecord::Base
  has_many :ratings
  has_many :rated_items, :through => :ratings, :source => :rateable, :source_type => "Item"
end

# file: app/models/rating.rb
class Rating < ActiveRecord::Base
  belongs_to :user
  belongs_to :rateable, :polymorphic => true
  validates_uniqueness_of :user_id, :scope => :rateable
end

With this your User can rate different Items, but only once per Item.

To implement this you'd need rateable_id and rateable_type fields in your ratings table. Plus, off course, user_id and rating_score or something.

The great advantage is that you are able to add as much rateables as you wish. If, for example, you want to rate a Song model, then you can simply implement it like this:

# file: app/models/song.rb
class Song < ActiveRecord::Base
  has_many :ratings, :as => :rateable
end

To show ratings for an Item: item.ratings To show all items rated by a User: user.rated_items

P.S. I am not good in English grammar, so correct me if have misspelled rateable
P.P.S. this implementation is untested, I wrote it straight out of my head, so there's no guarantee it will work 100% :)

Good luck!

Ivan
  • 874
  • 10
  • 32
  • @ivan I think I'm following. I have view new.html.erb with this: <%= form_for(@rating) do |f| %> <%= render 'fields', :f => f %>
    <%= f.submit "Rate" %>
    <% end %> and _rating_form.html.erb with: <%= form_for @rating do |f| %> <%= render 'shared/error_messages', :object => f.object %>
    <%= f.text_area :rating_score %>
    <%= f.submit "Submit" %>
    <% end %> So somehow I would need to pull in the rateable_id and rateable_type (not sure this is) and anything else? I am very new to this.
    – Scott Apr 21 '11 at 04:10
  • @scott First, I think I didn't understand what "Rating" should do, I thought it should assign an Integer value between, for ex, 0 and 10 to an Item. But the general concept of polymorphic doesn't change if you want to write a review about an Item. So for new rating form I would do something like this: `<%= form_for(@item, @item.ratings.new) do |f| %>` `<%= f.text_area :rating_text %>` `<%= f.submit 'Submit' %>` `<% end %>` Rails figures `rateable_id` and `rateable_type` itself from the association `@item.ratings.new`. – Ivan Apr 21 '11 at 08:32
  • @scott One more thing: Two things to keep in mind to simplify form creation: 1) you might want to use HAML - it's a simplified markup language 2) you might want to use `Formtastic` or `Simple Form` gems to easily create your forms – Ivan Apr 21 '11 at 08:38
  • @ivan Thanks a ton. Basically my site will list items and the user can click on an item to see details as well as give their own rating in different categories.. hence the ratings form. – Scott Apr 21 '11 at 09:01
  • @ivan Actually there will me multiple ratings per category and possibly a text area as you said. I assume I would just add something like ratings_score1, ratings_score2, etc, to my ratings table and then have separate form areas for each like <%= f.text_area :ratings_score1 %> etc, etc. and then submit. – Scott Apr 21 '11 at 09:13
  • @ivan Filled out the contact form on your site as well as sent email to both addresses. – Scott Apr 21 '11 at 09:36