uniqueness with nested_form_for rails is not working -


i have models vehicle, tag, vehicletag

class vehicletag < activerecord::base   attr_accessible :vehicle_id, :tag_id, :capacity    belongs_to :vehicle   belongs_to :tag    validates_uniqueness_of :tag_id, :scope => [:vehicle_id] end 

and have used 'nested_form_for' enter value in vehicle_tags tables validates_uniqueness_of not working when user select same tags multiple times , saving data in tables.

but when have single record in vehicle_tag tag_id=2, vehicle_id=24 , when user select same tag again time thronging uniqueness validation. getting first time don't have value in db , second time have.

but want through uniqueness when user select multiple same tag.

edit: vehicle_tag table structure:

+-----+------------+--------+----------+---------------------+---------------------+ | id  | vehicle_id | tag_id | capacity | created_at          | updated_at          | +-----+------------+--------+----------+---------------------+---------------------+ | 241 |          2 |      2 |        2 | 2014-04-22 11:35:11 | 2014-04-22 11:35:11 | | 242 |          2 |      2 |        3 | 2014-04-22 11:35:11 | 2014-04-22 11:35:11 | +-----+------------+--------+----------+---------------------+---------------------+  class vehicle < activerecord::base   has_many :vehicle_tags   has_many :tags, :through => :vehicle_tags    accepts_nested_attributes_for :vehicle_tags, :reject_if => proc { |a| a['capacity'].blank? }, allow_destroy: true end  class tag < activerecord::base   has_many :vehicle_tags   has_many :vehicles, :through => :vehicle_tags end 

you may try this, in seperate line

validates :tag_id, :uniqueness => true 

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -