Hi, Can anyone take a look at this.... i''ve been banging my head on the wall for several days trying to figure this out. I can''t figure out why the validation is bypassed. The only clue I have is it seems its not evaluating :question_type == "standard" to true. Model: validates :question_type, :presence => true if :question_type == "standard" validates :question, :presence => true ... Controller: def create @question = Question.new(params[:question]) if @question.save if params[:question_type] == "standard" flash[:success] = "Question created!" redirect_to @question else ..... new.html.erb: <%= f.submit ''Save'', question_type: "standard" ... -- 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 unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On 1 March 2013 13:20, Dave Castellano <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Hi, > > Can anyone take a look at this.... i''ve been banging my head on the > wall for several days trying to figure this out. I can''t figure out why > the validation is bypassed. The only clue I have is it seems its not > evaluating :question_type == "standard" to true. > > Model: > validates :question_type, :presence => true > if :question_type == "standard" > validates :question, :presence => trueIs that code just inline in the model? If so then it is only executed once, when the code is loaded, so the if statement is not tested for each object being validated. Have a look at the Rails Guide on ActiveRecord Validations and Callbacks to see ways that you can accomplish what you want. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Hi Dave, I believe you should do something along the lines of Model: validates_each :question do |record, attr, value| record.errors.add attr, ''The question is not present!?'' if value.blank? and record.question_type=="standard" end plz correct me if I''m wrong, Colin :) cheers, Walther Den 01/03/2013 kl. 14.20 skrev Dave Castellano <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>:> Hi, > > Can anyone take a look at this.... i''ve been banging my head on the > wall for several days trying to figure this out. I can''t figure out why > the validation is bypassed. The only clue I have is it seems its not > evaluating :question_type == "standard" to true. > > Model: > validates :question_type, :presence => true > if :question_type == "standard" > validates :question, :presence => true > ... > > Controller: > def create > @question = Question.new(params[:question]) > if @question.save > if params[:question_type] == "standard" > flash[:success] = "Question created!" > redirect_to @question > else > ..... > > new.html.erb: > <%= f.submit ''Save'', question_type: "standard" ... > > -- > 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 unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit https://groups.google.com/groups/opt_out. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On 1 March 2013 13:46, Walther Diechmann <walther-KMX8Ls7xMCscWVvVuXF20w@public.gmane.org> wrote:> Hi Dave, > > I believe you should do something along the lines of > > Model: > > validates_each :question do |record, attr, value| > record.errors.add attr, ''The question is not present!?'' if value.blank? and record.question_type=="standard" > end > > plz correct me if I''m wrong, Colin :)It might be easier to use :if Colin> > cheers, > Walther > > > Den 01/03/2013 kl. 14.20 skrev Dave Castellano <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>: > >> Hi, >> >> Can anyone take a look at this.... i''ve been banging my head on the >> wall for several days trying to figure this out. I can''t figure out why >> the validation is bypassed. The only clue I have is it seems its not >> evaluating :question_type == "standard" to true. >> >> Model: >> validates :question_type, :presence => true >> if :question_type == "standard" >> validates :question, :presence => true >> ... >> >> Controller: >> def create >> @question = Question.new(params[:question]) >> if @question.save >> if params[:question_type] == "standard" >> flash[:success] = "Question created!" >> redirect_to @question >> else >> ..... >> >> new.html.erb: >> <%= f.submit ''Save'', question_type: "standard" ... >> >> -- >> 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 unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> For more options, visit https://groups.google.com/groups/opt_out. >> >> > > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit https://groups.google.com/groups/opt_out. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On Friday, March 1, 2013 1:20:24 PM UTC, Ruby-Forum.com User wrote:> > Hi, > > Can anyone take a look at this.... i''ve been banging my head on the > wall for several days trying to figure this out. I can''t figure out why > the validation is bypassed. The only clue I have is it seems its not > evaluating :question_type == "standard" to true. > > Model: > validates :question_type, :presence => true > if :question_type == "standard" > validates :question, :presence => true > ... > > This code is evaluated when the model is loaded. It compares the symbol:question_type to the string ''standard'' (which are of course never equal) and so doesn''t add the validation. You want the choice of whether to apply the validation or not to be taken when the model is about to be validated. The standard way of doing that is with the :if option validates :question_type, :presence => true validates :question, :presence => true, :if => :standard_question? will call the standard_question? method and only enforce the presence validation on question if the method returns true. There is also a lambda form validates :question, :presence => true, :if => lambda {|record| record.question_type == ''standard''} Fred -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/0GDkOcyyom4J. For more options, visit https://groups.google.com/groups/opt_out.
Ok, Thank you. I read Rails Guide on ActiveRecord Validations and Callbacks, specifically Conditional Validation. So, validates :question, :presence => true, :if => "question_type.blank?" placed inline in model should be the simplest way to do this. It does not work however. My lack of understanding seems to be related to how I specify question_type in the condition? As you can see i''m still on the bottom of the steep part of the learning curve.'' Thanks for the help -- 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 unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Frederick Cheung wrote in post #1099720:> On Friday, March 1, 2013 1:20:24 PM UTC, Ruby-Forum.com User wrote: >> if :question_type == "standard" >> validates :question, :presence => true >> ...> with the :if option > > validates :question_type, :presence => true > validates :question, :presence => true, :if => > :standard_question? > > will call the standard_question? method and only enforce the presence > validation on question if the method returns true. There is also a > lambda > form > > validates :question, :presence => true, :if => lambda {|record| > record.question_type == ''standard''} > > FredThanks Fred, Got this error undefined method `standard_question?'' for #<Question:0x007f947ddb2e00> Do I need to write a standard_question method (I''m beginner) -- 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 unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Lambda method works, although I don''t understand it :-) Thanks!! Dave -- 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 unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
On Friday, March 1, 2013 2:58:53 PM UTC, Ruby-Forum.com User wrote:> > > with the :if option > > > > validates :question_type, :presence => true > > validates :question, :presence => true, :if => > > :standard_question? > > > > will call the standard_question? method and only enforce the presence > > validation on question if the method returns true. There is also a > > lambda > > form > > > > validates :question, :presence => true, :if => lambda {|record| > > record.question_type == ''standard''} > > > > Fred > > Thanks Fred, > Got this error > undefined method `standard_question?'' for #<Question:0x007f947ddb2e00> > Do I need to write a standard_question method > > Yes - it''s up to you to write a standard_question? method. The lambdasaves you having to have the condition in a separate method but can make your validation look more cluttered (especially for more complicated conditions). Fred -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/sgk504Zwsz4J. For more options, visit https://groups.google.com/groups/opt_out.
Thanks again, Is there a way to add a message if validation fails using lambda method (I am using Lambda method until I can figure out why validates :question, :presence => true, :if =>:standard_question? generates undefined method `standard_question?'' for #<Question:0x007f947ddb2e00>) -- 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 unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
Dave, you have to add an instance method "standard_question?" to your model def standard_question? end from the url below you can see that you can pass a :message param to customize the error message http://guides.rubyonrails.org/active_record_validations_callbacks.html#message On Fri, Mar 1, 2013 at 11:13 AM, Dave Castellano <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org>wrote:> Thanks again, > > Is there a way to add a message if validation fails using lambda method > (I am using Lambda method until I can figure out why > validates :question, :presence => true, :if > =>:standard_question? > generates > undefined method `standard_question?'' for #<Question:0x007f947ddb2e00>) > > -- > 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 unsubscribe from this group and stop receiving emails from it, send an > email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit https://groups.google.com/groups/opt_out. > > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.
You want the choice of whether to apply the validation or not to be taken when the model is about to be validated. http://www.hqew.net -- 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 unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit https://groups.google.com/groups/opt_out.