0

I want to create a record in join table but rails shows me two errors in two situation, and I don't want to generate a third model.

@channel = Channel.find(params[:channel_id])
 if @channel.users.create!(channel_id: params[:channel_id], user_id: params[:user_id])
     flash[:success] = "U Succeed:)"
     redirect_to request.referrer
 else
     flash[:danger] = "U Nit Succeed:H"
     redirect_to request.referrer
 end

second situation

 if Channel.users.create!(channel_id: params[:channel_id], user_id: params[:user_id])
     flash[:success] = "U Succeed:)"
     redirect_to request.referrer
 else
     flash[:danger] = "U'r Not Succeed:H"
     redirect_to request.referrer
 end

I want to save attrs in join table. According to rails official site guide, what's wrong?

First error:

unknown attribute 'channel_id' for User.

Second error:

undefined method `users' for Class:0x00007feaa0312058

iLuvLogix
  • 5,920
  • 3
  • 26
  • 43
  • Can you post your model's code for each? Also for the second scenario, it's an `undefined method 'users' for Class0x00007..` bc the association is on an instance of Channel, not the class itself. – Int'l Man Of Coding Mystery May 03 '19 at 07:33

3 Answers3

1

I am assuming that you have associations like these:

class User < ActiveRecord::Base
  has_and_belongs_to_many :channels
end

class Channel < ActiveRecord::Base
  has_and_belongs_to_many :users
end

Now you are trying to do like this:

@channel.users.create!(channel_id: params[:channel_id], user_id: params[:user_id])

This will try to create a new User class object as there is no Model in between you just have a mid table. Instead you can do it like this:

# If you don't have the user object already
user = User.find params[:user_id]

# This will create a record in the mid table
@channel.users << user

This will create a new record in the mid table and the existing records will also exist as it is. And if you do like this:

@channel.users = user

This will delete all the existing associated user records from the mid table for this channel and add a new associated record with this user.

And when you try doing like this:

Channel.users.create!(channel_id: params[:channel_id], user_id: params[:user_id])

This is not valid at all because the class Channel doesn't have any direct relation with the User but an instance of Channel class may be associated with instances of User class.

Deepesh
  • 6,138
  • 1
  • 24
  • 41
0

For the first scenario i would suggest you should do it like

@channel.user_ids = @channel.user_ids  + [params[:user_id]]

it will create join table records, you can surely try optimised approach for this as you see fit.

Ashok Damaniya
  • 303
  • 1
  • 6
0

you can use push or << method instead of create : Channel.users.push(attrs) or Channel.users << (attrs) and second answer in good too but .ids not very readable or you can find channel by id and use it : channel.users.create(attrs)

see api.rubyonrails.org and search has_and_belongs_to_many methods in searchbar