I would include this functionality in the before_create
callback function in the user model, since it is essentially the model logic, will not add another save call, and will usually be more elegant.
One of the possible reasons why your code is not working is because @profile = Profile.create
not running successfully because it is not checking or something else. This will cause @profile.id
be nil
and thus @user.default_card
will be nil
.
Here is how I could implement this:
class User < ActiveRecord::Base ... before_create :create_profile def create_profile profile = Profile.create self.default_card = profile.id
In your code (or mine) you can always put a simple puts
to check if a new profile is being created. those. puts (@profile = Profile.create)
source share