Unwanted call to validates_associated

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

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

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.

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

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs