class Product < ActiveRecord::Base has_many :collaborators has_many :users, :through => :collaborators end
class User < ActiveRecord::Base has_many :collaborators has_many :products, :through => :collaborators end
`class Collaborator < ActiveRecord::Base
belongs_to :product
belongs_to :user
validates :product_id, presence: true
validates :user_id, presence: true
end`
I’m having problems with the validate part in through model. When I create a new record from the User model
User.create!(name: "user name", product_ids: [1, 2, 3])
=> It returns the error Collaborator is in valid.
The problem here is that validates :user_id, presence: true
blocks this. And if I comment this line again, it works normally again.
The question is, should we put validate user_id and validate product_id in the through model?
Should we add validation to through model in Rails?