Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
私はアクティブレコードに不慣れです。関連付けのhas_oneとの違いを知りたいですbelongs_to。
has_one
belongs_to
誰かが良い例で説明できますか?
2番目のモデルでhas_one使用しない関連付けの例が必要です。belongs_to
違いは、外部キーが格納される場所にあります。
たとえば、 が にpost属しているuser場合、投稿テーブルには列があり、投稿がどのユーザーに属してuser_idいるかがわかります。
post
user
user_id
has_onesome_user.post のようなメソッドのみを利用可能にします。
参照: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#label-Is+it+a+belongs_to+or+has_one+association%3F