<?xml version="1.0" encoding="UTF-8"?>
<post>
  <body>Hey there. Let's assume you have some set like this

&lt;pre&gt;&lt;span class=&quot;keyword&quot;&gt;class &lt;/span&gt;&lt;span class=&quot;class&quot;&gt;User&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;constant&quot;&gt;ActiveRecord&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;constant&quot;&gt;Base&lt;/span&gt;
  &lt;span class=&quot;ident&quot;&gt;has_one&lt;/span&gt; &lt;span class=&quot;symbol&quot;&gt;:profile&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;symbol&quot;&gt;:dependent&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;symbol&quot;&gt;:destroy&lt;/span&gt;
&lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;


&lt;span class=&quot;keyword&quot;&gt;class &lt;/span&gt;&lt;span class=&quot;class&quot;&gt;Profile&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;constant&quot;&gt;ActiveRecord&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;constant&quot;&gt;Base&lt;/span&gt;
  &lt;span class=&quot;ident&quot;&gt;belongs_to&lt;/span&gt; &lt;span class=&quot;symbol&quot;&gt;:user&lt;/span&gt;
  &lt;span class=&quot;ident&quot;&gt;validates_presence_of&lt;/span&gt; &lt;span class=&quot;symbol&quot;&gt;:first_name&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;symbol&quot;&gt;:last_name&lt;/span&gt;
&lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;

&lt;/pre&gt;

&lt;p&gt;
  And you create the profile in the same view as your user.
  How do you validate those first_name and last_name fields
  with a friendly message for your user?
  &lt;br /&gt;
  Well, you could use &lt;a href=&quot;http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M001642&quot;&gt;
  validates_associated&lt;/a&gt;, but you would end up with a message like &quot;Profile is
  invalid&quot;. Now, how friendly is that?
&lt;/p&gt;

&lt;p&gt;
To obtain a more precise message, use this on you user.rb
&lt;/p&gt;

&lt;pre&gt;&lt;span class=&quot;keyword&quot;&gt;def &lt;/span&gt;&lt;span class=&quot;method&quot;&gt;validate&lt;/span&gt;
  &lt;span class=&quot;keyword&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;constant&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;profile&lt;/span&gt;
    &lt;span class=&quot;constant&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;profile&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;errors&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;each&lt;/span&gt; &lt;span class=&quot;keyword&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;punct&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;|&lt;/span&gt;
      &lt;span class=&quot;constant&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;errors&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;add_to_base&lt;/span&gt; &lt;span class=&quot;ident&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;ident&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;('&lt;/span&gt;&lt;span class=&quot;string&quot;&gt; &lt;/span&gt;&lt;span class=&quot;punct&quot;&gt;')&lt;/span&gt;
    &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;keyword&quot;&gt;end&lt;/span&gt;
&lt;/pre&gt;</body>
  <created-at type="datetime">2008-11-13T15:07:43Z</created-at>
  <id type="integer">39</id>
  <permalink>how-to-get-better-error-messages-for-a-associated-model</permalink>
  <title>How to get better error messages for an associated model. </title>
  <updated-at type="datetime">2008-11-13T23:38:49Z</updated-at>
  <user-id type="integer">1</user-id>
</post>
