多个嵌套的accepts_nested_attributes和“save”之间的区别和“创建”

发布于 2024-12-16 11:08:26 字数 1192 浏览 6 评论 0原文

我很难理解为什么使用这些模型与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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

楠木可依 2024-12-23 11:08:26

这里发生了很多事情:

  • has_many :through 的
  • 两个接受双向多态
  • 关联的嵌套

属性我的技巧(正如您所问的)是以敏捷的方式一一处理这些事情。
在进行下一步之前,先让每个部分都可以创建、编辑和查看。
这将对你的前进有很大帮助。

  • 首先让 has_many 正常工作。
  • 然后让嵌套属性正常工作 - 并从一个nested_attributes 开始。
  • 最后研究多态关联 - 但请注意,许多人避免或放弃多态关联,因为尽管它们看起来很简单,但对某些人来说它们很繁琐。我不向 Rails 新手推荐它们,尤其是像这样组合时。

最后要注意的是,除非你先阅读/研究一下 Rails,并且有很多相关的网络资源,否则这些东西没有多大意义。

我会尝试开始这样的事情:

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

现在允许图片出现在您可能不需要的多个页面和书籍中,但这是一个更“标准”的 has_many 通过并且会起作用,所以我会从这个开始。

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.

顾冷 2024-12-23 11:08:26

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文