Gabriel Saravia
2009-May-13 01:14 UTC
Setting a boolean to false in a before_create callback
Has anyone ever had trouble doing this? Whenever I try to do so, it doesn''t seem to work: in my model: [code] before_create :set_defaults def set_defaults self.submitted = false end [/code] in my rspec test: [code] it "Should set ''submitted'' to false before a create" do @assignment = Assignment.new(:submitted => true) @assignment.save @assignment.submitted.should be_false end [/code] and the result: [code] Assignment Should set ''submitted'' to false before a create'' FAILED expected false, got true [/code] -- Posted via http://www.ruby-forum.com/.
Matt Jones
2009-May-13 17:07 UTC
Re: Setting a boolean to false in a before_create callback
Does the object pass validation? before_create callbacks won''t run if it doesn''t. I''d recommend changing the middle line to @assignment.save.should be_true or equivalent to check if the record is getting saved at all. --Matt Jones On May 12, 8:14 pm, Gabriel Saravia <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Has anyone ever had trouble doing this? Whenever I try to do so, it > doesn''t seem to work: > > in my model: > [code] > before_create :set_defaults > > def set_defaults > self.submitted = false > end > [/code] > > in my rspec test: > [code] > it "Should set ''submitted'' to false before a create" do > @assignment = Assignment.new(:submitted => true) > @assignment.save > @assignment.submitted.should be_false > end > [/code] > > and the result: > [code] > Assignment Should set ''submitted'' to false before a create'' FAILED > expected false, got true > [/code] > -- > Posted viahttp://www.ruby-forum.com/.
Frederick Cheung
2009-May-13 17:28 UTC
Re: Setting a boolean to false in a before_create callback
On 13 May 2009, at 18:07, Matt Jones wrote:> > Does the object pass validation? before_create callbacks won''t run if > it doesn''t. > > I''d recommend changing the middle line to @assignment.save.should > be_true or equivalent to check if the record is getting saved at all. >Also, be careful as if a before_create evaluates to false (as your one does) the create won''t actually happen. Fred> --Matt Jones > > On May 12, 8:14 pm, Gabriel Saravia <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: >> Has anyone ever had trouble doing this? Whenever I try to do so, it >> doesn''t seem to work: >> >> in my model: >> [code] >> before_create :set_defaults >> >> def set_defaults >> self.submitted = false >> end >> [/code] >> >> in my rspec test: >> [code] >> it "Should set ''submitted'' to false before a create" do >> @assignment = Assignment.new(:submitted => true) >> @assignment.save >> @assignment.submitted.should be_false >> end >> [/code] >> >> and the result: >> [code] >> Assignment Should set ''submitted'' to false before a create'' FAILED >> expected false, got true >> [/code] >> -- >> Posted viahttp://www.ruby-forum.com/. > >
Gabriel Saravia
2009-May-13 17:40 UTC
Re: Setting a boolean to false in a before_create callback
> Also, be careful as if a before_create evaluates to false (as your one > does) the create won''t actually happen.a-ha! thanks to both of you! I had no validations on submitted because I was trying to isolate the problem (but that doesn''t mean i didn''t want any), but indeed, it was the before_create evaluating to false. final version: before_validation_on_create :set_defaults def set_defaults self.submitted = false return (self.submitted == false) end Also, is it just me because I''m too new, or is the "before_validation_on_create" callback not talked about enough? Cheers, -Gabe -- Posted via http://www.ruby-forum.com/.
Robert Walker
2009-May-13 18:16 UTC
Re: Setting a boolean to false in a before_create callback
Gabriel Saravia wrote:> Also, is it just me because I''m too new, or is the > "before_validation_on_create" callback not talked about enough?As me for me, I set my defaults in the migration (and hence the database) rather than initializing attributes with callbacks. I don''t know if that''s exactly the right way to do it, but it seems to work well as far as I can tell. ... ... t.boolean :bool_value, :default => false ... ... -- Posted via http://www.ruby-forum.com/.