1

Topic: Model Relationships

Hi, i have two models; user and profile each of which have correct correct relations set.

I am using an auth gem (devise) however I a record for profile must be inserted for each user, is this a quick fix? should this be done?

My user model looks like this,

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable,
         :token_authenticatable, :confirmable, :lockable, :timeoutable
         
  attr_accessible :email, :password, :password_confirmation, :remember_me 
  has_one :profile, :dependent => :destroy
end

Re: Model Relationships

Just before saving a user, assign it a corresponding profile:

class User < ActiveRecord::Base
  before_save :assign_profile

  private
    def assign_profile
      self.profile = whatever_you want from your Profile model
   end
end