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 1 year has passed since last update.

子モデルを作成するbuildメソッド

Posted at

1 対 1

# user model
 has_one :item

# item model
 belongs_to :user
@user = User.new
@item = @user.build_item

1 対 多

# user model
 has_many :item

# item model
 belongs_to :user
@user = User.new
@item = @user.items.build

多 対 多

# user model
  has_many :group_users
  has_many :groups, through: :group_users

# group model
  has_many :group_users
  has_many :users, through: :group_users

# group_user model (中間テーブル)
  belongs_to :group
  belongs_to :user
@user = User.new
@group = @user.groups.build
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?