我有类似的东西:
class Project < ActiveRecord::Base
has_many :foos, inverse_of: :project
has_many :bars, inverse_of: :project
end
class Foo < ActiveRecord::Base
belongs_to :project, inverse_of: :foos
has_and_belongs_to_many :bars
accepts_nested_attributes_for :bars
end
class Bar < ActiveRecord::Base
belongs_to :project, inverse_of: :bars
has_and_belongs_to_many :foos
validates_presence_of :project
end
proj = Project.create
foo = Foo.new(project: project, bars_attributes: {0 => [name: "first bar"]})
foo.save! #-> Bar is invalid
Foos和Bars都属于一个项目,并且habtm关系加入了它们......
我希望能够为Bar创建新的Foos,让它们属于Bar的项目......但是项目验证失败了。
我是通过ActiveAdmin进行此操作,但是我正试图在模型/关系级别解决它,然后再使用InheritedResources覆盖进行hacky ...