title: Rails validation for nested attributes in just one especific model
body: <p>I am new regarding validations of nested attributes for an especific model and I am finding it quite impossible to solve.</p>
  
  <p>I have a <code>Pharmacy model</code> as per below:</p>
  
  <pre><code>class Pharmacy &lt; ActiveRecord::Base
  
    belongs_to :address
  
    accepts_nested_attributes_for :address
  
    validates :address, presence: true
  
    validate :address_postcode_town
  
    def address_postcode_town
     errors.add(:'address.postcode', 'Postcode can't be blank') if address&amp;.postcode.blank?
     errors.add(:'address.town', "Town can't be blank") if address&amp;.town.blank?
    end
  end
  </code></pre>
  
  <p>I also have an <code>Address model</code> that currently look like this:</p>
  
  <pre><code>class Address &lt; ActiveRecord::Base
  
  validates :first_line, presence: true
  end
  </code></pre>
  
  <p>As you can see, address validates presence of first line, and I need to add validation on postcode for <code>Pharmacy</code>, and here comes the part when I get lost.</p>
  
  <p>I need validation of postcode, which is an attribute of <code>Address</code>, only for my <code>Pharmacy</code> and not for my <code>Order</code></p>
  
  <p>I tried to add <code>validates :postcode, presence: true</code> in my <code>Address</code> model but then it is a requirement for Order as well, which I don't want to happen.</p>
  
  <p>I have also tried a <code>before_validation</code> in my <code>Pharmacy</code> model but not sure if that works, as the view is not showing the <code>postcode can't be blank</code> message:</p>
  
  <pre><code>before_validation :ensure_address_has_a_value
  
  def ensure_address_has_a_value
    return false if address.blank?
    !address.postcode.blank? || !address.city.blank?
  end
  </code></pre>
  
  <p>How can I add validation for nested attributes only in one model, leaving the other as they are?</p>
  
  <p>I have managed to create a custom validation that works, but now the view does not recognise <code>postcode</code> nor <code>town</code> as a required attributes, therefore id does not show the error message. It just renders the form again.</p>
  
  <p><code>params</code> are available in the <code>PharmacyController</code> and my view looks like below:</p>
  
  <pre><code>**simple_form_for :pharmacy goes here**
  &lt;%= f.simple_fields_for :address do |a|%&gt;
    &lt;%= a.input :first_line, label: t('.first_line'), wrapper_html: {class: 'form-row'}%&gt;
    &lt;%= a.input :second_line, label: t('.second_line'), wrapper_html: {class: 'form-row'}%&gt;
    &lt;%= a.input :postcode, label: t('.postcode'), wrapper_html: {class: 'form-row'} %&gt;
    &lt;%= a.input :town, label: t('.town'), wrapper_html: {class: 'form-row'} %&gt;
  </code></pre>
  
  <p>This did not help me <a href="https://stackoverflow.com/questions/16074689/validation-for-nested-attributes">Validation for nested attributes</a></p>
  
  <p>Can anyone shed some light on this? Many thanks in advance!!</p>