views:

662

answers:

1

When submitting a nested object form, I can't get it to reject invalid child objects because the reject_if proc doesn't see the Paperclip attribute.

Here are the relevant parts of my models & forms:

class Stage < ActiveRecord::Base
  has_and_belongs_to_many :assets, :uniq => true
  accepts_nested_attributes_for :assets, :reject_if => lambda { |attrs| attrs['asset'] }

  ...
end

class Asset < ActiveRecord::Base
  has_attached_file :asset, :path => "#{Rails.root}/public/:attachment/:id/:style/:basename.:extension",
                             :url => ":attachment/:id/:style/:basename.:extension"
  validates_attachment_presence :asset
 end

 - form_for [@campaign, @stage], :html => {:multipart => true} do |f|
 ....
   - f.fields_for :assets do |asset_form|
     - field_set_tag do
       - if asset_form.object.new_record?
         %h4 New Asset
       %p
         = asset_form.label :asset, "File"
         %br
         = asset_form.file_field :asset
       %p
         = asset_form.label :identifier
         %br
         = asset_form.text_field :identifier

I put a debugger in the reject_if part:

(rdb:1) p attrs
{"identifier"=>""}

I thought it was because it only looks at attributes that are columns but that's not it either, as I found by adding an attr_accessor to Asset

p attrs
{"misc_attr"=>"", "identifier"=>""}

I could go on and on with the weird ins & outs but I want to get this out there and see who's had this problem before.

A: 

Hi David,

I do not know if this is the point, but shouldn´t there be in the Asset class a line that declares the association to the Stage class like:

    class Asset < ActiveRecord::Base
    has_and_belongs_to_many :stages

??

Trientalis