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 5 years have passed since last update.

class_name

Posted at

class_nameとは

アソシエーションのオプションの一つで、関連づけするモデルの名前を変えたいときに使用します。
通常

post.rb
class Pweet < ActiveRecord::Base

belongs_to :user
has_many :comments

end

と表記することでuser、commentsと関連づけすることができますが

class_nameを使用すると

post.rb
class Post < ActiveRecord::Base

belongs_to :owner, class_name: 'User'
has_many :comments

end

と表記することでuserの内容のままownerと関連づけることが可能です。

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?