多个嵌套的accepts_nested_attributes和“save”之间的区别和“创建”
我很难理解为什么使用这些模型与accepts_nested_attributes_for 时“保存”和“创建”应该有所不同。这是我的模型:
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
首先,使用 save 方法......
b = Book.first
params = { "page_attributes"=> { "number"=>"1" }}
p = b.picture_pages.new(params)
p.save
事情就像你期望的那样工作。 Rails 将自动创建一个新的 PicturePage,以及相应的带有指定“number”属性的 Page join 模型。完美的。但如果我这样做:
b = Book.first
params = { "page_attributes"=> { "number"=>"1" }}
p = b.picture_pages.create(params)
...Rails 将创建两个连接模型,一个完全为空,另一个具有 number 属性。这是为什么呢?
如果我想在书籍模型上使用accepts_nested_attributes_for,这是一个主要问题,因为书籍模型将在它正在创建的PicturePage模型上自动调用“create”。
有什么建议吗?这是 Rails 中的错误吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里发生了很多事情:
属性我的技巧(正如您所问的)是以敏捷的方式一一处理这些事情。
在进行下一步之前,先让每个部分都可以创建、编辑和查看。
这将对你的前进有很大帮助。
最后要注意的是,除非你先阅读/研究一下 Rails,并且有很多相关的网络资源,否则这些东西没有多大意义。
我会尝试开始这样的事情:
现在允许图片出现在您可能不需要的多个页面和书籍中,但这是一个更“标准”的 has_many 通过并且会起作用,所以我会从这个开始。
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:
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.
create
:创建一个对象(或多个对象)并将其保存到数据库中。save
:如果模型是新的,则会在数据库中创建一条记录,否则现有记录将被更新。基本上,您无法使用 create 更新特定记录,但使用 save 可以。但对于新记录,两者都可以使用,这取决于您是想用一行代码还是两行代码来完成。
create
: Creates an object (or multiple objects) and saves it to the database.save
: If the model is new a record gets created in the database, otherwise the existing record gets updated.Basically you cannot update a particular record with create, but with save you can. But for a new record both can be used, it depends whether you want to do it in one line of code or two.