Portrait issue for accepts_nested_attributes_for and reject_if

I am developing an application for rails 3.

class Post < ActiveRecord::Base
  has_many :attachments
  has_many :photos
  accepts_nested_attributes_for :attachments, :allow_destroy => true, :reject_if => proc { |attrs| attrs['document'].blank? }
  accepts_nested_attributes_for :photos, :allow_destroy => true, :reject_if => proc { |attrs| attrs['image'].blank? }
end

class Attachment < ActiveRecord::Base
  belongs_to :post      
  has_attached_file :document
end

class Photo < ActiveRecord::Base
  belongs_to :post      
  has_attached_file :image, :styles => {
                                         :thumb  => "100x100#",
                                         :small  => "150x150>",
                                         :mid    => "640x640>",
                                         :large  => "800x800>"
                                       }

end

The problem is that "_destroy" => "1" does not work for attachments and photos. I realized that if I remove the reject_if option, it will work. What's wrong?

Thank.

Sam

+2
source share
1 answer

It seems like with Rails 3.0.3 you need to download the association you want to destroy (attachments, photos). Check out this ticket . A quick fix, which is not so elegant, is to load your association into your update method:

@post = Post.includes(:attachments).find(params[:id])

if @post.update_attributes(params[:post])
  redirect_to(posts_url, :notice => 'Post updated.'
else
  render :action => "edit"
end

FYI is still necessary for Rails 3.0.4.

+1
source

All Articles