Possible Duplicate:
Correct Way to Set Default Values in Rails
I have two data tables:
1) User
2) Profile (which has the field user_id)
They are associated through:
- User has_one Profile
- Profile belongs_to User
Is there a possibility to save some default values in the profile table each time I create a new user?
Thanks for your help!
You can create default profile by using ActiveRecord callbacks.
Just create a method, and use it as :after_create
build_profile builds and links an instace of Profile, but doesn’t save it. create_profile is the same, but it also saves the object. For the full description, see the ActiveRecord documentation.
You can add attributes to both build_ and create_profile as a hash, so you probably can reduce create_default_profile to one line: