LoginSignup
0
1

More than 3 years have passed since last update.

中間テーブルが更新されない

Posted at

解決方法

optional: trueをつける!

参考: http://soccer1356abc.hatenablog.com/entry/2018/09/22/210221

user.rb
  has_many :talent_users
  has_many :talents, through: :talent_users
talent.rb

  has_many :talent_users
  has_many :users, dependent: :destroy, through: :talent_users
talent_users.rb
  belongs_to :user
  belongs_to :talent
  belongs_to :shop, optional: true

user登録時にtalentも紐づけてcreateしたかった
この時shopは関係ないからoptional: trueをつける

ネストさせる

route.rb

    resources :users do
      resource :talent_users, only: [:create]
    end

パラメーターに登録したいタレントのidが入るようにしておけばOK!

users_controller.rb

  def user_params
    params.require(:user).permit(
      :email, :password, :tel ,:sex,
      :first_name, :last_name, :first_name_kana, :last_name_kana, { :talent_ids=> [] })
  end
0
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
0
1