title: Ruby on Rails - has_many :through with nested_attributes_for
body: <p>I'm coming across a problem that I can't find much online for via Google, forums, groups, etc. and so I'm going to raise my hand and ask for help from those who are more wise than I :)</p>
  
  <p>I have a rails project setup that was using nested_attributes_for with a one-to-one relationship between two models.  It worked quite easily and as expected until I recently had to modify the project to become a has_many :through relationship.</p>
  
  <p>The code looks something like this:</p>
  
  <h2>P model:</h2>
  
  <pre><code>has_many :f_ps  
  has_many :fs, :through =&gt; :f_ps
  </code></pre>
  
  <h2>F model:</h2>
  
  <pre><code>has_many :f_ps  
  has_many :ps, :through =&gt; :f_ps
  </code></pre>
  
  <h2>F_P model:</h2>
  
  <pre><code>belongs_to :p  
  belongs_to :f  
  validates_uniqueness_of :f_id, :scope =&gt; :p_id, :message =&gt; 'each F must be unique for this P'
  </code></pre>
  
  <p>As long as the code remains like above everything works fine (except of course CRUD actions via the web GUI).  However, as soon as I add in the nested attributes portion to the P model like so:</p>
  
  <pre><code>accepts_nested_attributes_for :fs,  
    :allow_destroy =&gt; true,  
    :reject_if =&gt; proc { |a| a.all? { |k,v| v.blank? } }
  </code></pre>
  
  <p>It causes my tests to fail and the GUI works (minus the validations are not enforced).</p>
  
  <p>Any ideas as to the proper way to go about getting this validation to work happily along with nested_attributes_for or do I just need to create my own CRUD actions in the P controller to handle it like a year or so ago?</p>