开发者

Using accepts_nested_attributes_for with rejects_if

开发者 https://www.devze.com 2023-01-11 08:50 出处:网络
I\'m using paperclip to attach photos to one of my mode开发者_JS百科ls: class User < ActiveRecord::Base

I'm using paperclip to attach photos to one of my mode开发者_JS百科ls:

class User < ActiveRecord::Base
  has_many :photos
  accepts_nested_attributes_for :photos
end

class Photo < ActiveRecord::Base
  belongs_to :user
  has_attached_file :data
end

How can I use reject_if to ignore data fields to which files are not uploaded by users?


Try:

accepts_nested_attributes_for :photos, :reject_if => proc { |attrs| attrs['data'].blank? }

That should effectively ignore any data fields which are left blank by the user.

0

精彩评论

暂无评论...
验证码 换一张
取 消