Re: Setting a boolean to false in a before_create callback

Frederick Cheung
Wed, 13 May 2009 17:28:37 -0000

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/.
> >