Hamd

Joined

60 Experience
0 Lessons Completed
0 Questions Solved

Activity

Posted in insert id into join table

My models :

class Document < ApplicationRecord
belongs_to :category
belongs_to :user
has_one_attached :document_file
has_many :document_users
has_many :users, :through => :document_users, dependent: :destroy

class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable,:lockable,:trackable

has_many :document_users
has_many :document, :through => :document_users, dependent: :destroy
has_many :documents

class DocumentUser < ApplicationRecord
self.table_name = "documents_users"
self.primary_key = [:document_id, :user_id]
belongs_to :document
belongs_to :user

My controller document :

@document = current_user.documents.new(document_params)

respond_to do |format|
if @document.save

   @document.documents_users << :document_users

   format.html { redirect_to @document, notice: "Document est créé." }
   format.json { render :show, status: :created, location: @document }
 else
   format.html { render :new, status: :unprocessable_entity }
   format.json { render json: @document.errors, status: :unprocessable_entity }
 end

end
end

into my form : <%= f.association :document_users, collection: User.all,label_method: :username,value_method: :id, input_html: { multiple: true }%>

And my error : Mysql2::Error: Unknown column 'documents_users.[:document_id, :user_id]' in 'where clause'

Posted in insert id into join table

Hello,

I would like add current id (document) and many user_id into my join table.

I use rails 6, but i don't find a documentation for add user_id and document.id into my join table.

I have a model, table for join table into my form i have f.association.

But i don't find syntax for insert many user_id when a document is created.

I have a topic into StackOverFlow if you want more explanations.