Reputation: 25
Hi I am creating 3 models with ruby rails But I have some problem. Here's my model code
class Company < ActiveRecord::Base
has_many :pendings
has_many :products, :through => :pendings
end
class Product < ActiveRecord::Base
has_many :pendings
has_many :companies, :through => :pendings
end
class Pending < ActiveRecord::Base
belongs_to :company
belongs_to :product
end
I wanted make it company can have many Products through Pending vice versa, It worked well, but is there any way to set only 1 pending model between company and product.
Upvotes: 1
Views: 33
Reputation: 4443
One option: you could leave the association as is, but add the following validation to pending.rb
validates :company_id, uniqueness: {scope: :product_id}
see more here: rails validation docs
This will ensure that you can only have one pending per company and product...but many pendings for the companies with regard to other products.
Upvotes: 1