Forum: Ruby on Rails Setting a boolean to false in a before_create callback

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
410ff5abeb4b1c686fe49d70dbba1c8f?d=identicon&s=25 Gabriel Saravia (theperuvian)
on 2009-05-13 03:14
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]
6883e5ef03484d4fcef507d7b4f1d243?d=identicon&s=25 Matt Jones (Guest)
on 2009-05-13 19:08
(Received via mailing list)
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...@andreas-s.net>
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-05-13 19:29
(Received via mailing list)
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
410ff5abeb4b1c686fe49d70dbba1c8f?d=identicon&s=25 Gabriel Saravia (theperuvian)
on 2009-05-13 19:40
> 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
280b78a61a968391b7e07e912be102a8?d=identicon&s=25 Robert Walker (robert4723)
on 2009-05-13 20:16
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
...
...
This topic is locked and can not be replied to.