rails association belongs_to -
i know sty must wrong in way build db please take min answering : building supermarket model, 1 user has shopping list, each list has many products. :
class user < activerecord::base has_many :lists end class list < activerecord::base belongs_to :user has_many :products end class product < activerecord::base ???? end
a list has several products products don't belong lists. should have users having many lists, , lists having many products ? regards,
have class links them via has_many through.
class listitem < activerecord::base belongs_to :list belongs_to :product end class list < activerecord::base belongs_to :user has_many :list_items has_many :products, through: :list_items end
ruby-on-rails associations belongs-to
No comments:
Post a Comment