title: Rails 3 - Custom validator
body: <p>I'm trying to make a custom validator work on my app.</p>
  
  <p>I have already configured my <code>config.autoload.paths</code> and it is loading fine.</p>
  
  <p>The problem is with the validator itself.</p>
  
  <p><em>Result of <code>binding pry</code></em></p>
  
  <pre><code>instance variables: @attributes  @options  @with
  locals: _  _dir_  _ex_  _file_  _in_  _out_  _pry_  attribute  record  value
  [12] pry(#&lt;FileCountValidator&gt;)&gt; value
  =&gt; [#&lt;Attachment id: 60, description: nil, file: "cache_600_1__img_948867_5770137d84a6c79ac825886938e...", attachable_type: "Post", attachable_id: 15, created_at: "2012-03-10 14:50:54", updated_at: "2012-03-10 14:50:54"&gt;,
   #&lt;Attachment id: 61, description: nil, file: "cache_600_1__img_948867_90f64e01b9c871ec656a884e015...", attachable_type: "Post", attachable_id: 15, created_at: "2012-03-10 14:50:54", updated_at: "2012-03-10 14:50:54"&gt;,
   #&lt;Attachment id: 62, description: nil, file: "cache_600_1__img_948867_85eda3946c27fa90566403ac941...", attachable_type: "Post", attachable_id: 15, created_at: "2012-03-10 14:50:54", updated_at: "2012-03-10 14:50:54"&gt;,
   #&lt;Attachment id: nil, description: nil, file: nil, attachable_type: "Post", attachable_id: 15, created_at: nil, updated_at: nil&gt;]
  [13] pry(#&lt;FileCountValidator&gt;)&gt; value &gt; @with
  TypeError: compared with non class/module
  from /home/kleber/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.1/lib/active_record/relation/delegation.rb:20:in `&gt;'
  [14] pry(#&lt;FileCountValidator&gt;)&gt; value.size &gt; @with
  =&gt; true
  [15] pry(#&lt;FileCountValidator&gt;)&gt; value.size
  =&gt; 4
  [16] pry(#&lt;FileCountValidator&gt;)&gt; @with
  =&gt; 3
  [17] pry(#&lt;FileCountValidator&gt;)&gt; 
  </code></pre>
  
  <p>So, I trying to make this comparisson exactly like I did on the pry debug console.</p>
  
  <pre><code>def validate_each(record, attribute, value)
    #binding.pry
     record.errors.add(attribute,"#{@with} attachments per post only. #{attribute['file'].size} detected.") if value.size &gt; @with
    end
  </code></pre>
  
  <p>But doing this, return me the error:</p>
  
  <pre><code>NoMethodError (undefined method `size' for nil:NilClass):
    lib/validators/file_count_validator.rb:11:in `validate_each'
    app/controllers/posts_controller.rb:61:in `block in update'
    app/controllers/posts_controller.rb:60:in `update'
  </code></pre>
  
  <p>Are there any way to catch the <code>value</code> before it get enters the <code>validate_each</code> method?</p>