Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested form splitting child records across multiple rows #58

Open
sdee opened this issue Mar 14, 2014 · 2 comments
Open

Nested form splitting child records across multiple rows #58

sdee opened this issue Mar 14, 2014 · 2 comments

Comments

@sdee
Copy link

sdee commented Mar 14, 2014

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:

+----+----------+---------------+-----------------+------+
| id | store_id | as_product_id | as_product_type | cost |
+----+----------+---------------+-----------------+------+
| 45 |     NULL | 29            | Book            |   17 |
| 46 |       39 | 29            | Book            | NULL |
+----+----------+---------------+-----------------+------+

The association and attribute (cost) gets split across two rows. Any ideas?

The params passed through the form looks normal to me:

 Parameters: {"utf8"=>"✓", "authenticity_token"=>"aHmLdcXZxW/BSU0XIbW7PwGWXNJ2cX42NtMkDCEN7GE=", "store"=>{"name"=>"Borders", "books_attributes"=>{"0"=>{"author"=>"Gaiman", "cost"=>"17.0", "_destroy"=>"false"}}}, "commit"=>"Create Store"}
@rkamun1
Copy link

rkamun1 commented May 17, 2014

Hi, I'm getting the same weird behaviour. Any luck? (Rails 4.1.0/Ruby 2.1.1)

@rkamun1
Copy link

rkamun1 commented May 18, 2014

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

@book = @store.book.build
@product = @book.build_product

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:

@store.products.collect(&:product).sort{|x,y| x.cost <=> y.cost} 

If you find out how to sort as a default scope, please let me know

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants