0
私は2つのActiveRecords Author
とBook
を持っています。条件付き子供の検証をスキップ
class Author < ActiveRecord::Base
has_many :books
enum author_type: {
musician: 0,
scientist: 1
}
accepts_nested_attributes_for :books
end
class Book < ActiveRecord::Base
belongs_to :author
validates :name, presence: true
validates :score_url, presence: true
end
Book
、name
とscore_url
の両方に存在することを検証します が、私はauthor.author_type
がscientist
ときscore_url
の検証をスキップします。
この方法で試しましたが、作成中にauthor
が見つかりませんでした。
class Book < ActiveRecord::Base
belongs_to :author
validates :name, presence: true
validates :score_url, presence: true, if: "author.scientist?"
end
ここで最適な解決策は何ですか?