对于具有嵌套现有记录的新关联记录,AcitveRecord不会使用accept_nested_attributes_

wul*_*one 8 activerecord ruby-on-rails nested-attributes

ActiveRecord似乎并不理解,给定具有嵌套属性的现有记录的一组参数,它可以创建一个新的嵌套记录,该记录本身具有嵌套的现有记录.(关系树:Existing -> New -> Existing)

这是一个错误,还是我错过了什么?

让我举个简单的例子.

这是我的模特:

class User < ActiveRecord::Base
  has_many :posts
  attr_accessible :name, :posts_attributes
  accepts_nested_attributes_for :posts
end

class Post < ActiveRecord::Base
  belongs_to :group
  belongs_to :user
  attr_accessible :content, :title, :group_attributes
  accepts_nested_attributes_for :group
end

class Group < ActiveRecord::Base
  has_many :posts
  attr_accessible :name
end
Run Code Online (Sandbox Code Playgroud)

我在每个表中都创建了一条记录,并相应地将它们相关联,因此每个表中都有一条记录 - 这id=1是已知的.现在,如果我有一个现有用户,一个新帖子和一个现有组,并尝试使用accepts_nested_attributes_for它更新该记录,它不喜欢它:

1.9.3-p125 :044 > params
{
                  :id => 1,
                :name => "Billy",
    :posts_attributes => [
        [0] {
                          :title => "Title",
                        :content => "Some magnificent content for you!",
            :group_attributes => {
                  :id => 1,
                :name => "Group 1"
            }
        }
    ]
}
1.9.3-p125 :045 > u
#<User:0x00000002f7f380> {
            :id => 1,
          :name => "Billy",
    :created_at => Fri, 03 Aug 2012 20:21:37 UTC +00:00,
    :updated_at => Fri, 03 Aug 2012 20:21:37 UTC +00:00
}
1.9.3-p125 :046 > u.update_attributes params
   (0.1ms)  begin transaction
   (0.1ms)  rollback transaction
ActiveRecord::RecordNotFound: Couldn't find Group with ID=1 for Post with ID=
  from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/nested_attributes.rb:462:in `raise_nested_attributes_record_not_found'
  from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/nested_attributes.rb:332:in `assign_nested_attributes_for_one_to_one_association'
  from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/nested_attributes.rb:288:in `group_attributes='
  from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/attribute_assignment.rb:94:in `block in assign_attributes'
  from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/attribute_assignment.rb:93:in `each'
  from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/attribute_assignment.rb:93:in `assign_attributes'
  from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/base.rb:498:in `initialize'
  from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/reflection.rb:183:in `new'
  from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/reflection.rb:183:in `build_association'
  from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/associations/association.rb:233:in `build_record'
  from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/associations/collection_association.rb:112:in `build'
  from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/nested_attributes.rb:405:in `block in assign_nested_attributes_for_collection_association'
  from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/nested_attributes.rb:400:in `each'
  from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/nested_attributes.rb:400:in `assign_nested_attributes_for_collection_association'
  from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/nested_attributes.rb:288:in `posts_attributes='
  from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/attribute_assignment.rb:85:in `block in assign_attributes'
  from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/attribute_assignment.rb:78:in `each'
  from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/attribute_assignment.rb:78:in `assign_attributes'
  from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/persistence.rb:216:in `block in update_attributes'
  from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status'
  from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
  from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/transactions.rb:208:in `transaction'
  from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/transactions.rb:293:in `with_transaction_returning_status'
  from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.7/lib/active_record/persistence.rb:215:in `update_attributes'
  from (irb):15
  from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.7/lib/rails/commands/console.rb:47:in `start'
  from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.7/lib/rails/commands/console.rb:8:in `start'
  from /home/trevor/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.7/lib/rails/commands.rb:41:in `<top (required)>'
  from script/rails:6:in `require'
  from script/rails:6:in `<main>'1.9.3-p125 :047 > 
Run Code Online (Sandbox Code Playgroud)

它认为它无法找到与新​​帖子相关的组(具有已知ID).当我从中删除ID时它会起作用group_attributes(但它会创建一个新的组记录).它在我提供posts_attributesID 时起作用,并从中删除id group_attributes(并再次创建一个新组).它们都有ID时也可以使用.

这种关系正在起作用:

1.9.3-p125 :059 > p = Post.new( { group_attributes: { name: 'Testing' } } )
#<Post:0x00000004212380> {
            :id => nil,
         :title => nil,
       :content => nil,
      :group_id => nil,
       :user_id => nil,
    :created_at => nil,
    :updated_at => nil
}
1.9.3-p125 :060 > p.group
[
    [0] #<Group:0x00000004211868> {
                :id => nil,
              :name => "Testing",
        :created_at => nil,
        :updated_at => nil
    }
]
Run Code Online (Sandbox Code Playgroud)

如果所有记录都是新记录,它在使用posts_attributes和创建group_attributes期间也完全有效User.

它不应该仍然在第一个例子中工作吗?ActiveRecord应该足够聪明才能弄明白......!

Yar*_*boy 4

我认为正在发生的事情是这样的:您正在传递一个组的 ID,向 ActiveRecord 表明该组存在。ActiveRecord 正在尝试找到该组以使用group_attributes中的其他数据更新它。由于您是post_attributes中执行此操作,因此ActiveRecord 会尝试通过帖子之间的关联来找到该组。也就是说,ActiveRecord 首先查找关联的组 - 其中 id = post.group_id - 然后从该结果中查找 ID = 1 的组。对于父关系来说,这可能看起来有点奇怪和笨拙,就像您的情况一样,但是我确信您可以在另一个方向上看到这是有用的行为,其中嵌套属性代表潜在的多个子级中的一个或多个。

但是,根据 post_attributes 中的数据创建的帖子对象尚未与组关联 - post.group_id 为零。因此,当 ActiveRecord 进行第一次搜索以获取关联组时,它会显示为空。相应地,它在(空)结果中找不到 ID = 1 的组。从技术上讲,记录是存在的,但就与帖子的关联而言,它并不存在。

您可以通过在 post_attributes 中包含 group_id => 1 来证明这一点。我相信,如果您这样做,ActiveRecord 将通过关联找到该组,然后从结果中成功子选择 ID = 1 的组,然后使用 group_attributes 中的附加数据更新该组。

另请注意,像您所做的那样在帖子内包含组的嵌套属性的唯一原因是您允许用户在创建新帖子的同时更新组名称。如果您想要做的只是将新帖子链接到现有组,那么您只需在 post_attributes 中包含 group_id 即可删除 group_attributes。