Search code examples
ruby-on-railsnestedassociationsfields-for

Multiple nested accepts_nested_attributes and difference between "save" and "create"


I'm having troubles understanding why "save" and "create" should be any different using these models with accepts_nested_attributes_for. This is my models:

class Book < ActiveRecord::Base
  has_many :pages
  has_many :picture_pages, :through => :pages, :source => :pagetype, :source_type => 'PicturePage'
  accepts_nested_attributes_for :picture_pages
end

class PicturePage < ActiveRecord::Base
  has_one :page, :as =>:pagetype
  has_one :book, :through => :pages
  accepts_nested_attributes_for :page
end

class Page < ActiveRecord::Base
  belongs_to :book
  belongs_to :pagetype, :polymorphic => true, :dependent => :destroy
end

First of all, using the save method....

b = Book.first
params = { "page_attributes"=> { "number"=>"1" }}
p = b.picture_pages.new(params)
p.save

... things work like you'd expect. Rails will automatically create a new PicturePage, with a corresponding Page join model with an assigned "number" attribute. Perfect. But if I do this:

b = Book.first
params = { "page_attributes"=> { "number"=>"1" }}
p = b.picture_pages.create(params)

... Rails will create TWO join models, one that is completely empty and one that has the number attribute. Why is this?

This is a major problem if I want to use the accepts_nested_attributes_for on the book model, because then the Book model will call "create" automatically on the PicturePage models it's creating.

Any tips? Is this a bug in Rails?


Solution

  • You have a lot of things going on here:

    • has_many :through 's
    • two accepts nested attributes going both ways
    • polymorphic association

    My tip (as you asked) is to tackle these things one by one in an agile fashion. Get each piece working for create and edit and view before doing the next. This will help you greatly going forward.

    • First get the has_many through's working.
    • Then get the nested attributes working - and start with just one nested_attributes.
    • Finally work on the polymorphic association - but note that many folks avoid or abandon polymorphic associations as they are fiddly for some despite their apparent simplicity. I do not recommend them for rails newbies especially if combined like this.

    A final note is that this stuff will not make a lot of sense unless you read up / study rails a bit first and there a lot of web resources for that out there.

    I would try to start something like this:

    class Book < ActiveRecord::Base
      has_many :page_pictures
      has_many :pictures, :through => :page_pictures
    end
    
    class PagePicture < ActiveRecord::Base
      belongs_to :book
      belongs_to :picture
    end
    
    class Picture < ActiveRecord::Base
      has_many :page_pictures
      has_many :books, :through => :page_pictures
    end
    

    Now that allows for Pictures to be in multiple pages and books which you probably don't need, but this is a more 'standard' has_many through and will work so I would start with this.