sombe's technique is right, but his details aren't ideal. In fact, since create_user_info
is already a method on User
instances, all you want is something like:
class User < ActiveRecord::Base
has_one :user_info
before_create :create_user_info
end
Edit: init
doesn't do anything particularly magical under Rails (I... don't think it does under basic Ruby either - are you thinking of initialize
? I'll assume you are). initialize
is fired off when an instance of the Ruby class is created in memory. That's divorced by quite some margin from an instance of the model being created in the database; a new class instance could be due to you calling build
(and not saving yet), or even due to reading an instance out of the database.
If you want to step in on database operations, you need to make use of the ActiveRecord callbacks. You might find my answer to this question useful.