0
0

More than 1 year has passed since last update.

#Rails で DBに外部キー制約がない場合 has_one と has_many のアソシエーションを両方指定できそうだ

Last updated at Posted at 2020-01-13

You can specify both

class User < ApplicationRecord
  has_one :book
  has_many :books
end

userが持つbookが複数ある場合にhas_oneを見ると、最新のbook一個だけが得られた

user.books.size # 3

user.book # Return latest book instance a user has

Original by Github issue

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0