基本的には、
参照先名_idのカラムを持つモデルがbelongs_to
参照先モデルがhas_one
となる。
Userモデルと、Profileモデルで
■Profileモデルにuser_idがある場合
class User < ActiveRecord::Base
has_one :profile
end
class Profile < ActiveRecord::Base
belongs_to :user
end
has_one :profile
end
class Profile < ActiveRecord::Base
belongs_to :user
end
■Userモデルにprofile_idがある場合
class User < ActiveRecord::Base
belongs_to :profile
end
class Profile < ActiveRecord::Base
has_one :user
end
belongs_to :profile
end
class Profile < ActiveRecord::Base
has_one :user
end
ruby on rails - what's the difference between belongs_to and has_one? - Stack Overflow
ヽ( ・∀・)ノくまくまー(2006-01-20)
タグ:Rails