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