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?
You have a lot of things going on here:
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.
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.