views:

16

answers:

1

Hi folks, 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?

+1  A: 

Sure do.

Your callbacks are, as you know, just method names. Therefore rather than using create_laser here you could call another method here to setup and create a laser with some default parameters. Let's call it setup_laser and use it like this:

before_create :setup_laser

We'd define it in the Geek model like this:

private

def setup_laser
  create_laser(:power => 5000)
end
Ryan Bigg
Thanks Ryan this looks nice!
tabaluga