0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Rails]joinsについて

Posted at

仕様をすごく勘違いしてたのでメモに残しておく。

テーブルBがテーブルAに所属している(Bがa_idというカラムを持つ)ならば、
A.joins(:bs).select("as.*, bs.*")
=>a_idで紐付いた一つの大きなテーブルができる

と思っていたが、違う・・・

モデルの定義で、
A has_many: bs
B belongs_to: a

という関係ならば、

A.joins(:bs).select("as.*, bs.*")
または
B.joins(:a).select("as.*, bs.*")
=>A.id = B.a_idで紐付いた一つの大きなテーブルができる
※joinsの引数の複数形、単数形に注意。モデルの定義を参照。

って感じなんすね。
逆でもいけるんだね。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?