Rails - Need help setting up loosely coupled associations -
Rails - Need help setting up loosely coupled associations -
i have next models, relevant associations:
class user < activerecord::base has_many :reviews has_many :ratings end class product < activerecord::base has_many :reviews has_many :ratings end class review < activerecord::base belongs_to :product belongs_to :user end class rating < activerecord::base belongs_to :product belongs_to :user end given specific rating, need corresponding review (if review exists).
i need maintain ratings , reviews loosely coupled. (i not want set model review belongs_to rating)
how should set rating's association reviews?
once i'm working specific rating in view, can phone call @rating.product.reviews.where(:user_id => @rating.user.id).first, i'd cleaner/more efficient if possible.
any ideas?
thanks.
try using :conditions so:
class rating < activerecord::base has_many :reviews, :through => :user, :source => :reviews, :conditions => ['#{review.table_name}.product_id = #{product_id}'] end if doesn't work, instead (very much @robinbrouwer answer):
class rating < activerecord::base def reviews user.reviews.where(:product => product) end end ruby-on-rails ruby-on-rails-3 activerecord associations
Comments
Post a Comment