これ:
accepts_nested_attributes_for :photo,
:reject_if => proc { |attributes| attributes['image'].blank? },
:reject_if => proc { |attributes| attributes['photo_title'].blank? },
:allow_destroy => true
これと同じです:
accepts_nested_attributes_for :photo, {
:reject_if => proc { |attributes| attributes['image'].blank? },
:reject_if => proc { |attributes| attributes['photo_title'].blank? },
:allow_destroy => true
}
太い矢印の引数は実際にはハッシュであり、中括弧は基本的に Ruby によって背後で追加されます。ハッシュは重複キーを許可しないため、2 番目の:reject_if
値が最初の値を上書きし、次のようになります。
accepts_nested_attributes_for :photo,
:reject_if => proc { |attributes| attributes['photo_title'].blank? },
:allow_destroy => true
ただし、両方の条件を 1 つの Proc で組み合わせることができます。
accepts_nested_attributes_for :photo,
:reject_if => proc { |attributes| attributes['image'].blank? || attributes['photo_title'].blank? },
:allow_destroy => true
別の方法を使用することもできます。
accepts_nested_attributes_for :photo,
:reject_if => :not_all_there,
:allow_destroy => true
def not_all_there(attributes)
attributes['image'].blank? || attributes['photo_title'].blank?
end