ruby on rails - A Model That Contains Other Models -


i may have terminology bit off, i'm going try explain as possible.

i'm working on application has different types of products: suits, shoes, shirts. these separate models don't have similar point of inheritance. user, owns collection, should able add 1 or many of these collection.

i thinking of using has_many :through, not seem elegant. have create 3 similar joining tables each model (or think). there better solution? , if solution requires modify structure is, there solution now?

thank in advance.

if user own collection, suits, shoes , shirts must belong user. also, need use nested attributes. so, you'll have like:

user.rb

has_many :suits has_many :shoes has_many :shirts accepts_nested_attributes_for :suits accepts_nested_attributes_for :shoes accepts_nested_attributes_for :shirts 

shirts

belongs_to :user 

and than, can use form code add products:

form_for @user |f|  f.fields_for :shoes, @user.shoes.new |builder|   builder.text_field :price   ...  end  f.fields_for :suits, @user.suits.new |builder|   builder.text_field :price   ...  end end 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -