Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby on Rails: How to validate nested attributes on certain condition?
    primarykey
    data
    text
    <p>I have these models:</p> <pre><code>class Organisation &lt; ActiveRecord::Base has_many :people has_one :address, :as =&gt; :addressable, :dependent =&gt; :destroy accepts_nested_attributes_for :address, :allow_destroy =&gt; true end class Person &lt; ActiveRecord::Base attr_accessible :first_name, :last_name, :email, :organisation_id, :address_attributes belongs_to :user belongs_to :organisation has_one :address, :as =&gt; :addressable, :dependent =&gt; :destroy accepts_nested_attributes_for :address, :allow_destroy =&gt; true # These two methods seem to have no effect at all! validates_presence_of :organisation, :unless =&gt; "address.present?" validates_associated :address, :unless =&gt; "organisation.present?" end class Address &lt; ActiveRecord::Base belongs_to :addressable, :polymorphic =&gt; true validates_presence_of :line1, :line2, :city, :zip end </code></pre> <p>...and these views:</p> <p><strong>_fields.html.erb</strong>:</p> <pre><code>&lt;%= render 'shared/error_messages', :object =&gt; f.object %&gt; &lt;fieldset&gt; &lt;div class="left"&gt; &lt;%= f.label :first_name %&gt;&lt;br/&gt; &lt;%= f.text_field :first_name %&gt; &lt;/div&gt; &lt;div&gt; &lt;%= f.label :last_name %&gt;&lt;br/&gt; &lt;%= f.text_field :last_name %&gt; &lt;/div&gt; &lt;div&gt; &lt;%= f.label :email %&gt;&lt;br/&gt; &lt;%= f.text_field :email %&gt; &lt;/div&gt; &lt;div&gt; &lt;%= f.label :organisation_id %&gt;&lt;br/&gt; &lt;%= f.select(:organisation_id, current_user.organisation_names, {:include_blank =&gt; "--- None ---"}, :id =&gt; 'organisation_select') %&gt; &lt;/div&gt; &lt;/fieldset&gt; &lt;%= f.fields_for :address do |address| %&gt; &lt;%= render 'shared/address', :f =&gt; address %&gt; &lt;% end %&gt; </code></pre> <p><strong>_address.html.erb:</strong></p> <pre><code>&lt;fieldset id="address_fields"&gt; &lt;div&gt; &lt;%= f.label :line1 %&gt; &lt;%= f.text_field :line1 %&gt; &lt;/div&gt; &lt;div&gt; &lt;%= f.label :line2 %&gt; &lt;%= f.text_field :line2 %&gt; &lt;/div&gt; &lt;div&gt; &lt;%= f.label :zip %&gt; &lt;%= f.text_field :zip %&gt; &lt;/div&gt; &lt;div&gt; &lt;%= f.label :city %&gt; &lt;%= f.text_field :city %&gt; &lt;/div&gt; &lt;/fieldset&gt; </code></pre> <p><strong>people_controller.rb</strong>:</p> <pre><code>def new puts params.inspect @person = Person.new(:organisation_id =&gt; params[:organisation_id]) @person.build_address @title = "New person" end {"action"=&gt;"new", "controller"=&gt;"people"} def edit puts params.inspect @title = @person.name end {"action"=&gt;"edit", "id"=&gt;"69", "controller"=&gt;"people"} def create puts params.inspect if params[:organisation_id] @person = current_user.organisations.build_person(params[:person]) else @person = current_user.people.build(params[:person]) end if @person.save flash[:success] = "Person created." redirect_to people_path else render :action =&gt; "new" end end {"commit"=&gt;"Create", "action"=&gt;"create", "person"=&gt;{"last_name"=&gt;"Doe", "organisation_id"=&gt;"9", "email"=&gt;"john.doe@email.com", "first_name"=&gt;"John", "address_attributes"=&gt;{"city"=&gt;"Chicago", "zip"=&gt;"12345", "line2"=&gt;"Apt 1", "line1"=&gt;"1 Main Street"}}, "authenticity_token"=&gt;"Jp3XVLbA3X1SOigPezYFfEol0FGjcMHRTy6jQeM1OuI=", "controller"=&gt;"people", "utf8"=&gt;"✓"} </code></pre> <p>Inside my <code>Person</code> model I need to make sure that only if a person's <code>organisation_id</code> is blank, that person's address fields have to be present.</p> <p>I tried something like this:</p> <pre><code>validates :address, :presence =&gt; true, :if =&gt; "organisation_id.blank?" </code></pre> <p>But it's not working.</p> <p>How can this be done?</p> <p>Thanks for any help.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload