In a rails project, I have 3 controllers and models, user, responsibility and profile. I have below code:
user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_one :responsibility
has_one :profile
before_create :build_responsibility
before_create :build_profile
end
responsibility.rb
class Responsibility < ActiveRecord::Base
belongs_to :user
end
profile.rb
class Profile < ActiveRecord::Base
belongs_to :user
validates :user_id, uniqueness: true
validates_numericality_of :nic_code, :allow_blank => true
validates_numericality_of :phone_number
validates_length_of :phone_number, :minimum => 11, :maximum => 11
validates_length_of :nic_code, :minimum => 10, :maximum => 10, :allow_blank => true
has_attached_file :photo, :styles => { :medium => "300x300>", :thumb => "35x35>" }, :default_url => "profile-missing.jpg"
validates_attachment_content_type :photo, :content_type => [ 'image/gif', 'image/png', 'image/x-png', 'image/jpeg', 'image/pjpeg', 'image/jpg' ]
end
Now, when I created a new user, before_create
works for responsibility
and creates it, but for profile
it's not working and doesn't create a new profile. Is there a difference between profile
and responsibility
? Why before_create
works for responsibility
, but doesn't work for profile
?