LoginSignup
1
1

More than 5 years have passed since last update.

Rails has_manyとbelongs_toの宣言🔰

Last updated at Posted at 2017-01-22

has_manyとbelongs_toの使い方

userとpostの例

user.rb
class User < ActiveRecord::Base
  has_many :posts
end

ユーザーはpost(投稿)を何個も持つ(投稿を何度もできる)
そのため、複数形

post.rb
class Post < ActiveRecord::Base
  belongs_to :user
end

1つのpostに紐づくユーザーは1人しかいない
そのため、単数形

1
1
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
1
1