I’m using rails 2.3.8 and I have a model with two attributes I want to validate by.
I have version and file number and I need to validate uniqueness of the file number on update but only according to each individual unique version.
So I’ve tried
validates_uniqueness_of :file_number, :scope => :version, :on => :update
I repeatedly get “File number has already been taken”
There is no special logic with the file number except for in a before_create where I add one to the count of items for that version.
EDIT: Here’s the SQL that the server performs. http://pastie.org/private/ysbwtiyouhgqkjsjjbooa
The id here is the id of the record I’m updating and the one returned from the query is another record.
Any ideas?
EDIT- I got it!
SOLUTION:
Turns out the validation wasn’t looking across a select list of mymodel records which would be defined by
mymodel belongs to project and project has_many mymodel
so with the following tweak it worked.
validates_uniqueness_of :file_number, :scope => [:version, :project_id], :on => :update
Pasting solution as answer:
Turns out the validation wasn’t looking across a select list of mymodel records which would be defined by
mymodel belongs to project and project has_many mymodel
so with the following tweak it worked.
validates_uniqueness_of :file_number, :scope => [:version, :project_id], :on => :updat