1

The following is giving me issue:

accepts_nested_attributes_for :photo, :reject_if => proc { |attributes| attributes['image'].blank? }, :reject_if => proc { |attributes| attributes['photo_title'].blank? }, :allow_destroy => true 

I think it's because I'm calling :reject_if twice, not 100% sure. But when ever I uncomment the photo_title reject_if line my image doesn't get upload if I select one. If I comment the line out then it does.

How can I combine both conditions into one reject_if condition? If that makes sense.

Kind regards

1
  • 1
    why don't you use an extern method to cleanly handle the logic? Commented May 12, 2012 at 12:31

2 Answers 2

5

This:

accepts_nested_attributes_for :photo, :reject_if => proc { |attributes| attributes['image'].blank? }, :reject_if => proc { |attributes| attributes['photo_title'].blank? }, :allow_destroy => true 

is the same as this:

accepts_nested_attributes_for :photo, { :reject_if => proc { |attributes| attributes['image'].blank? }, :reject_if => proc { |attributes| attributes['photo_title'].blank? }, :allow_destroy => true } 

The fat-arrow arguments are actually a Hash, the braces are essentially added by Ruby behind your back. A Hash doesn't allow duplicate keys so the second :reject_if value overwrites the first one and you end up with this:

accepts_nested_attributes_for :photo, :reject_if => proc { |attributes| attributes['photo_title'].blank? }, :allow_destroy => true 

You can combine both conditions in one Proc though:

accepts_nested_attributes_for :photo, :reject_if => proc { |attributes| attributes['image'].blank? || attributes['photo_title'].blank? }, :allow_destroy => true 

You could also use a separate method:

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 
Sign up to request clarification or add additional context in comments.

Comments

1

Try this

accepts_nested_attributes_for :photo, :reject_if => proc { |attributes| attributes['image'].blank? || attributes['photo_title'].blank?}, :allow_destroy => true 

1 Comment

The same thing is happening so I guess theres a deeper issue. I have set :_destroy => "1" on both of those fields so not sure why it's not working.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.