views:

64

answers:

1

Hello, I am working on an application where a user has the ability to leave feedback on another user. Currently, I already have an implemented user model.

Is this considered a self referential association?

This seems a bit wierd to me (how to set it up through Active Record Associations).

How would I go about setting this association up, what type of association is it? Anything I need to watch out for while going about this (tips, suggestions, errors that may come up)?

According to Answers Below Would This be the Way?

(one to many between users and ratings and one to one between ratings and user)

class User < ActiveRecord::Base
    has_many :ratings
end

class Ratings < ActiveRecord::Base
    belongs_to :user
end
+4  A: 

This may not be in Rails/Active Record terms but ORM generic.

I would probably implement this by having a Feedback or Review model and your User model would have one-to-many Feedback/Reviews and the Feedback/Review would have a one-to-one relationship with the User who created it.

EDIT: Response to question update...

You can have an User object on the Ratings model that is the author. My rails experience is minimal but I believe your Ratings model would look like this.

class Ratings < ActiveRecord::Base
    belongs_to :user
    has_one :user
end
Flash84x
Agreed. Any time you find yourself associating a table with itself, you should stop and ask yourself if that association is a candidate for being a model of its own. In this case, it absolutely should be.
thanks guys. I updated my original question to a revised version. I'm not sure how to implement a one to one and one to many when a :user is used 2 times. For example in the one to many, you have a user that has_many :ratings and a Rating belongs_to a :user To me it seems from that, that i can't get the original user who submitted a rating on another user. What tables would I use in this case?Would the ratings table have the rating:integer feedback:text user_id:integer? But what about the receiver of the rating? How do I specify their data in there? Sorry I'm a bit confused. Hope its clear
bob
Check the update to my answer
Flash84x
thanks flash. I ended up getting it working. I will update with the code I used soon. I used the code I had put originally though to get it working. I did a nested association in my routes file. map.resources :users, :has_many => :ratingsthis did the trick when I had the association as in my original post. I appreciate the help though. Your answer helped me do it.
bob