this seems like a really simple Question…but behold 🙂
Geek name:string
Laser geek_id:integer, power:integer
Geek
has_one :Laser
end
Laser
belongs_to :Geek
end
simple enough, right?
Now I want to create the laser, after a geek gets created, so the new Geek Model looks like this
Geek
has_one :laser
after_create :create_laser
end
This works really nice, but I also like to pass a default value for the power attribute of the laser, so how do I do that?
after_create :create_laser(:power => 5000)
doesn’t work 🙁 but it looks nice 🙂
does anyone have a nice and elegant solution for this?
Sure do.
Your callbacks are, as you know, just method names. Therefore rather than using
create_laserhere you could call another method here to setup and create a laser with some default parameters. Let’s call itsetup_laserand use it like this:We’d define it in the
Geekmodel like this: