title: Check a condition before saving a nested object in Rails
body: <p>I have 3 model here : <code>NewWord</code>, <code>VerbForm</code> and <code>AdjForm</code>.</p>
  
  <p>In <code>NewWord</code> model , I have a column <code>word_type</code> stored type of word: <code>Adj Noun Verb Phrase GenericWord</code></p>
  
  <p>Each NewWord may have 1 VerbForm or 1 AdjForm</p>
  
  <pre><code>Class NewWord &lt; ApplicationRecord
  
      has_one :adj_form, dependent: :destroy
      has_one :verb_form, dependent: :destroy
      accepts_nested_attributes_for :adj_form, allow_destroy: true
      accepts_nested_attributes_for :verb_form, allow_destroy: true
  
     def self.types
          %w(Adj Noun Verb Phrase GenericWord)
     end
  end
  
  class NewWord::AdjForm &lt; ApplicationRecord
      belongs_to :new_word
  end
  
  class NewWord::VerbForm &lt; ApplicationRecord
      belongs_to :new_word
  end
  </code></pre>
  
  <p>I use this form to create a word alongside with it forms</p>
  
  <pre><code>&lt;%= simple_form_for new_word, remote: true do |f| %&gt;
      &lt;div class="error_section"&gt;&lt;/div&gt;
      &lt;%= f.input :word %&gt;
      &lt;%= f.input :kanji_version %&gt;
      &lt;%= f.input :word_type, collection: NewWord.types %&gt;
      &lt;%= f.simple_fields_for :verb_form do |v| %&gt;
          &lt;%= v.input :verb_type %&gt;
          &lt;%= v.input :dictionary_form %&gt;
          # Other fields
      &lt;% end %&gt;
      &lt;%= f.simple_fields_for :adj_form do |a| %&gt;
          &lt;%= a.input :adj_type %&gt;
          # Other fields
      &lt;% end %&gt;
      &lt;%= f.button :submit %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>My idea here is when user select <code>word_type</code> from dropdown, I can use Javasript to hide or show fields for <code>AdjForm</code> or <code>VerbForm</code>, or both. Then at submit, I only save <code>AdjForm</code> if new word's <code>word_type</code> is 'Adj', or <code>VerbForm</code> if <code>word_type</code> is 'Verb'. </p>
  
  <p>So, how can I achieve this ? Since nested object saved automatically when I run this in new word <code>create</code> method: <code>@new_word.save.</code> ? </p>
  
  <p>I have try <code>reject_if</code> but it only returns params for nested object only!</p>
  
  <pre><code>accepts_nested_attributes_for :adj_form, allow_destroy: true, reject_if: :not_adj
  
  def not_adj(att)
      att['new_word']['word_type'] != 'Adj'   # Found out "att" here only has attributes of AdjForm , not NewWord !
  end
  </code></pre>