LoginSignup
5
4

More than 5 years have passed since last update.

Railsのリレーションで紐づくカラム名を代替する

Posted at

Tealeaf AcademyにてRails再勉強はじめた。
内容はまだエントリーレベルだけど結構いろいろ発見があります。

1:mのリレーションを張るとき下記のように書くと外部キーのカラム名を使わず、
任意の名前で呼び出せる。

class Post < ActiveRecord::Base
belongs_to :creator, foreign_key: 'user_id', class_name: 'User'
end

これで下記のように投稿ユーザを呼び出せる↓

post = Post.first
post.creator

5
4
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
5
4