You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Every time I create a nested object that uses multiple table inheritance, two different rows is created for the child object.
I have a has_many relationship between my store and products object and using multiple table inheritance to represent books and other types of products. Every product has a cost attribute and belongs to a store.
class Store < ActiveRecord::Base
has_many :products, :dependent => :destroy
has_many :books, :through => :products, :source => :as_product, :source_type => "Book"
accepts_nested_attributes_for :products, :allow_destroy => true, :reject_if => :all_blank
accepts_nested_attributes_for :books, :allow_destroy => true, :reject_if => :all_blank
end
class Product < ActiveRecord::Base
acts_as_superclass
belongs_to :store
end
class Book < ActiveRecord::Base
belongs_to :store
acts_as :product, :include => true
end
When I build a store and nested products via a formtastic form, I end up getting these rows:
So here's what happens in your case. Do you have cost in your product model? If you do, then what happens is that upon creation of a book, it will run one build model to accommodate for the cost,
@book.product.build
which doesn't recognize the associated model(Store) and the default
which recognises the store and not the cost. Remove cost from your product and keep it in your book and you'll be fine. Assuming that you did this to sort by price, for example, you can the build helper methods to help you do this. eg:
Every time I create a nested object that uses multiple table inheritance, two different rows is created for the child object.
I have a has_many relationship between my store and products object and using multiple table inheritance to represent books and other types of products. Every product has a cost attribute and belongs to a store.
When I build a store and nested products via a formtastic form, I end up getting these rows:
The association and attribute (cost) gets split across two rows. Any ideas?
The params passed through the form looks normal to me:
The text was updated successfully, but these errors were encountered: