Soniya Ahuja
2010-Feb-25 00:35 UTC
Problem with validates_associated and validates_presence_of
This is really confusing me. I have some model similar to the one
below
class Question << ActiveRecord:Base
has_many :options
has_many :answers
validates_presence_of :answer
end
class Option << ActiveRecord:Base
belongs_to :question
has_one :answer
end
class Answer << ActiveRecord:Base
belongs_to :question
belongs_to :answer
validates_presence_of :option_id
end
What I am trying to achieve is something like this -
If a question has two options (option id 1 & 2) and the correct answer
is option 1, then an entry should be made in answer with the
corresponding question id and option id.
Now as shown above, if I put a validates_presence_of in answer, it
works fine when I play with it from the console. I create a new
option, do not save it and then create a new answer that is associated
with the option.
Example:
o1 = Option.new
a1 = Answer.new (:option => o1)
Now when I do a1.save or a1.save! they work fine and I get no errors
and the appropriate entries are made in the database. However, the
problem comes in when I try achieving this through the GUI.
In the GUI, I have a form for question and I''ve embedded a form for
Option through fields_for. Now, in the controller, based on some input
from GUI ( a hidden field that passes the option_sequence), I find the
option, create a new Answer like
answer = Answer.new(:option => option, :question => question)
and also added this to the question
question.answers << answer
Now all the three are new. When I click on Create/Submit button, I get
errors saying, Option can''t be blank and Answer is invalid.
However, if I remove the validation for presence of option, it works
and the option id as well as question id are correctly persisted.
Secondly, if I replace the validates_presence_of to check for :option
instead of option_id, even then it works.
I read in a ticket that option_id should be validated and not the
option. Besides, why does this work from the console and not from the
view. Is the code in the controller incorrect?
Any help is appreciated.
Thanks!
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Anjan Tek
2011-Nov-24 23:52 UTC
Re: Problem with validates_associated and validates_presence_of
Just in case anybody faces this problem: I solved it by using "validates_associated" to validate the presence of associations. Using "validates_presence_of" for this purpose gave me the same problems as the original poster. Anjan -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.