クラス
class Post < ActiveRecord::Base
accepts_nested_attributes_for :comments
accepts_nested_attributes_for :authors
has_many :comments
has_many :authors
end
class Author < ActiveRecord::Base
belongs_to :post
end
class Comment < ActiveRecord::Base
attr_accessible :disabled
belongs_to :post
before_create :set_disabled
def set_disabled
if self.post.authors.first.name == "Foo"
self.disabled == true
end
end
end
ネストされた属性を使用して新しい投稿を作成する
params = {
post: {
title: "A New Post",
comments_attributes: [
{ body: "This is a great post" }
],
authors_attributes: [
{name: "Foo"}
]
}
}
a = Post.create(params)
set_disabled
メモリ内にあるにもかかわらず、コメントが にアクセスできないため、コールバックでエラーが発生しpost.authors
ます。
現在の解決策は、 からそれらを取得することですObjectSpace
。これを行うためのより良い方法があるはずですか?