# Unwanted call to validates\_associated

**URL:** <https://www.ruby-forum.com/t/unwanted-call-to-validates-associated/177986>\
**Category:** Rails\
**Created:** [October 27, 2009, 2:24pm UTC](https://www.ruby-forum.com/t/unwanted-call-to-validates-associated/177986 "2009-10-27T14:24:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [October 27, 2009, 2:24pm UTC](https://www.ruby-forum.com/t/unwanted-call-to-validates-associated/177986/1 "2009-10-27T14:24:14Z")

</div>

In my schema, a question has\_many :answers. When the validations are  
run on a question, and one of the answers is not valid, then i get

“Answers is not valid”

coming out of errors.full\_messages. I don’t want this, i’m already  
testing the validity of the answers and this is an ugly and  
uninformative error message. It looks like it’s the message i would get  
back if i was calling

validates\_associated :answers

but i’m not, and if i _do_ add this line

validates\_associated :answers, :message =\> “are not all valid”

then i get “Answers are not all valid” AND “Answers is not valid”. So,  
i guess it’s coming from somewhere else. But i can’t work out where.  
With logging all i can see is that it happens during validation, and it  
happens _before_ any other custom validations that i specify. Ie,  
whatever is doing this seems to be doing it as the first step of the  
validation sequence. If i take out all of my validations then it still  
occurs. After doing that, the only reference to answers in the whole  
class is the association:

has\_many :answers, :class\_name =\> “MillionaireAnswer”, :order =\>  
“position”, :dependent =\> :destroy

The answers don’t add any errors to their parent question when they are  
validated, and there’s nothing going on in the controller to shove  
anything else in there. I could do something horrible and hacky and  
remove it from the error messages before showing them on the page but  
i’d rather understand what’s going on.

Anyone got any ideas? max  
In case it’s relevant here’s my classes for questions and answers.

require ‘fastercsv’

class MillionaireQuestion \< ActiveRecord::Base  
ALLOWED\_KINDS = %w(text graphic video audio multiple\_graphic  
multiple\_audio)

has\_many :question\_assets, :class\_name =\> “MillionaireQuestionAsset”,  
:order =\> “position”, :dependent =\> :destroy  
has\_many :assets, :through =\> :question\_assets, :order =\>  
“millionaire\_question\_assets.position”

has\_many :answers, :class\_name =\> “MillionaireAnswer”, :order =\>  
“position”, :dependent =\> :destroy

#answer\_ids= does not preserve the order so building them instead,  
which will set position correctly  
def answers=(hash\_array)  
self.answers.clear  
hash\_array.each do |hash|  
self.answers.build(hash)  
end  
end

#need to preserve order AND set from path or id  
def assets=(hash\_array)  
self.assets.clear  
hash\_array.each do |hash|  
if !hash[:id].blank?  
asset = Asset.find\_by\_id(hash[:id])  
elsif !hash[:path].blank?  
asset = Asset.find\_by\_path(hash[:path])  
else  
asset = nil  
end  
if asset  
self.assets \<\< asset  
end  
end  
end

def required\_asset\_count  
if self.kind == “text”  
return 0  
elsif [“multiple\_audio”, “multiple\_graphic”].include?(self.kind)  
return 4  
elsif [“audio”, “graphic”, “video”].include?(self.kind)  
return 1  
else  
return nil  
end  
end

end

class MillionaireAnswer \< ActiveRecord::Base  
acts\_as\_list :scope =\> :millionaire\_question

belongs\_to :question, :class\_name =\> “MillionaireQuestion”

named\_scope :correct, :conditions =\> [“correct = ?”, true]  
named\_scope :fifty\_fifty, :conditions =\> [“fifty\_fifty = ?”, true]

validates\_presence\_of :text  
validate :cannot\_be\_true\_and\_fifty\_fifty

def letter  
self.position ? %w(x A B C D)[self.position] : nil  
end

def cannot\_be\_true\_and\_fifty\_fifty  
errors.add\_to\_base(“Answer is marked as the correct answer and the  
fifty-fifty answer”) if self.correct && self.fifty\_fifty  
end  
end

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [October 28, 2009, 5:59am UTC](https://www.ruby-forum.com/t/unwanted-call-to-validates-associated/177986/2 "2009-10-28T05:59:35Z")

</div>

Max W. wrote:

> In my schema, a question has\_many :answers. When the validations are  
> run on a question, and one of the answers is not valid, then i get
> 
> “Answers is not valid”
> 
> coming out of errors.full\_messages. I don’t want this, i’m already  
> testing the validity of the answers and this is an ugly and  
> uninformative error message. It looks like it’s the message i would get  
> back if i was calling
> 
> validates\_associated :answers
> 
> but i’m not, and if i _do_ add this line
> 
> validates\_associated :answers, :message =\> “are not all valid”
> 
> then i get “Answers are not all valid” AND “Answers is not valid”. So,  
> i guess it’s coming from somewhere else. But i can’t work out where.  
> With logging all i can see is that it happens during validation, and it  
> happens _before_ any other custom validations that i specify. Ie,  
> whatever is doing this seems to be doing it as the first step of the  
> validation sequence. If i take out all of my validations then it still  
> occurs. After doing that, the only reference to answers in the whole  
> class is the association:
> 
> has\_many :answers, :class\_name =\> “MillionaireAnswer”, :order =\>  
> “position”, :dependent =\> :destroy
> 
> The answers don’t add any errors to their parent question when they are  
> validated, and there’s nothing going on in the controller to shove  
> anything else in there. I could do something horrible and hacky and  
> remove it from the error messages before showing them on the page but  
> i’d rather understand what’s going on.
> 
> Anyone got any ideas? max  
> In case it’s relevant here’s my classes for questions and answers.

Hi Max,

the scenario is that you are getting two error messages, one is  
generated (Answers are not all valid) and other one is default (Answers  
is not valid), and you are trying to remove the error message generated  
by default.

Please correct me if I am wrong.

Thanks  
Saurabh

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [October 28, 2009, 11:11am UTC](https://www.ruby-forum.com/t/unwanted-call-to-validates-associated/177986/3 "2009-10-28T11:11:45Z")

</div>

Hi Saurabh

Yes, it does seem that the “Answers is not valid” error message is  
default, in the sense that nothing that i am doing is causing it, and  
i’d like to remove it as you say. The fact that the error message is  
identical to what i would get from “validates\_associated :answers” makes  
me think it’s something to do with that but i may be wrong.

The weird thing is that i don’t usually get this in my other classes  
that have has\_many associations. I also don’t get it in this class for  
the association with assets, which is also has\_many. For example. if i  
invalidate an associated asset, or an associated question\_asset (which  
is the join between question and assets) then the question doesn’t mind,  
and is still valid.

So, it seems like a deviation from the normal behaviour.

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [October 28, 2009, 11:42am UTC](https://www.ruby-forum.com/t/unwanted-call-to-validates-associated/177986/4 "2009-10-28T11:42:43Z")

</div>

Max W. wrote:

> Hi Saurabh
> 
> Yes, it does seem that the “Answers is not valid” error message is  
> default, in the sense that nothing that i am doing is causing it, and  
> i’d like to remove it as you say. The fact that the error message is  
> identical to what i would get from “validates\_associated :answers” makes  
> me think it’s something to do with that but i may be wrong.
> 
> The weird thing is that i don’t usually get this in my other classes  
> that have has\_many associations. I also don’t get it in this class for  
> the association with assets, which is also has\_many. For example. if i  
> invalidate an associated asset, or an associated question\_asset (which  
> is the join between question and assets) then the question doesn’t mind,  
> and is still valid.
> 
> So, it seems like a deviation from the normal behaviour.

Hi Max,

Have you tried the following in your model:

def after\_validation  
# Skip errors that won’t be useful to the end user  
filtered\_errors = self.errors.reject{ |err| %w{ model name  
}.include?(err.first) }  
self.errors.clear  
filtered\_errors.each { |err| self.errors.add(\*err) }  
end

Hope this help.

Regards  
Saurabh

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [October 28, 2009, 11:50am UTC](https://www.ruby-forum.com/t/unwanted-call-to-validates-associated/177986/5 "2009-10-28T11:50:33Z")

</div>

Max W. wrote:

> Thanks, but that’s a workaround, and a rather complex one at that. I’d  
> rather understand why this is happening.

Hi Max,

I’m a newbie, not having much idea, still learning, So i used that  
workaround.

Regards  
Saurabh

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [October 28, 2009, 11:45am UTC](https://www.ruby-forum.com/t/unwanted-call-to-validates-associated/177986/6 "2009-10-28T11:45:30Z")

</div>

Thanks, but that’s a workaround, and a rather complex one at that. I’d  
rather understand why this is happening.
