Validates_uniqueness_of with the relation 'has_many through =>'

Rails 2.3.5

Below, I have a TicketMarket model where admins support "market" names. This table is linked to the ticket table through the association table.

I need for TicketMarket CRUD to be "validates_uniqueness_of", but as shown below, it will result in validation errors when someone creates a ticket and checks some market flags in the ticket form.

All I saw says the right thing is to hide the check based on the "ticket_id" field of the "ticket_market_associations" table:

validates_uniqueness_of :market_name, :case_sensitive => false, :scope => :ticket_id 

However, this does not work. With this scope, working with CRET TicketMarket results in the error "undefined method" ticket_id "for error #TicketMarket: 0x630bdf0.

What is the right thing to do so that the checks only go away when working with TicketMarket CRUD and not with Ticket?

Thanks!

 class TicketMarket < ActiveRecord::Base has_many :ticket_market_associations has_many :tickets, :through => :ticket_market_associations validates_presence_of :market_name validates_uniqueness_of :market_name, :case_sensitive => false end 
+4
source share

All Articles