在与inverse_of关联的两边使用嵌套属性

时间:2019-06-10 11:16:08

标签: ruby-on-rails ruby-on-rails-4 accepts-nested-attributes

我的Rails应用程序版本为4.1.16。我在模型的两侧都有嵌套属性的一对多关联,并且在两个模型中都有inverse_of选项。

class Post
  has_many :comments, inverse_of: :post
  accepts_nested_attributes_for :comments
end 


class Comment
  belongs :post, inverse_of: :comments
  accepts_nested_attributes_for :post
end 

在保存记录的同时触发无限递归(堆栈级深度错误)。

请找到相同的回溯记录:

activerecord (4.1.16) lib/active_record/autosave_association.rb:289:in `block in nested_records_changed_for_autosave?'
  activerecord (4.1.16) lib/active_record/autosave_association.rb:286:in `any?'
  activerecord (4.1.16) lib/active_record/autosave_association.rb:286:in `nested_records_changed_for_autosave?'
  activerecord (4.1.16) lib/active_record/autosave_association.rb:265:in `changed_for_autosave?'
  activerecord (4.1.16) lib/active_record/autosave_association.rb:289:in `block (2 levels) in nested_records_changed_for_autosave?'
  activerecord (4.1.16) lib/active_record/autosave_association.rb:289:in `any?'
  activerecord (4.1.16) lib/active_record/autosave_association.rb:289:in `block in nested_records_changed_for_autosave?'
  activerecord (4.1.16) lib/active_record/autosave_association.rb:286:in `any?'
  activerecord (4.1.16) lib/active_record/autosave_association.rb:286:in `nested_records_changed_for_autosave?'
  activerecord (4.1.16) lib/active_record/autosave_association.rb:265:in `changed_for_autosave?'
  state_machine (1.2.0) lib/state_machine/integrations/active_record.rb:491:in `changed_for_autosave?'
  activerecord (4.1.16) lib/active_record/autosave_association.rb:289:in `block (2 levels) in nested_records_changed_for_autosave?'
  activerecord (4.1.16) lib/active_record/autosave_association.rb:289:in `any?'
  activerecord (4.1.16) lib/active_record/autosave_association.rb:289:in `block in nested_records_changed_for_autosave?'

我调查了问题here。删除inverse_of可以解决此问题。但是我需要使用inverse_of选项。有没有解决此问题的方法?

0 个答案:

没有答案