Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #1559 (closed enhancement: fixed)

Opened 4 years ago

Last modified 4 years ago

[PATCH] validates_uniqueness_of scope request.

Reported by: jeremy.hopple@wholetechnology.com Assigned to: David
Priority: normal Milestone: 1.1
Component: ActiveRecord Version: 0.12.1
Severity: normal Keywords:
Cc:

Description

It would be great if you could validate the uniqueness of something by more than just one scope constraint. I'd love to say:

validates_uniqueness_of :cateogry_name, :scope => ["account_id", "category_id"]

Thanks!

Attachments

validates_uniqueness_of.rb (1.3 kB) - added by anonymous on 06/30/05 18:13:19.
array_scope_for_validates_uniqueness_of.diff (4.7 kB) - added by jeremy@jthopple.com on 12/01/05 19:51:44.
I finally got around
array_scope_for_validates_uniqueness_of.2.diff (4.8 kB) - added by jeremy@jthopple.com on 12/01/05 20:11:42.
Fixed some typos in my comments. Ignore the first diff.
array_scope_for_validates_uniqueness_of.3.diff (4.3 kB) - added by jeremy@jthopple.com on 12/01/05 20:16:13.
One more!!! My local connection changes are mistakenly in the second one.

Change History

06/30/05 18:10:25 changed by jeremy.hopple@wholetechnology.com

Sorry, I don't have time to follow setup instuctions for making an official patch diff, but I modified the validates_uniqueness_of function locally to accept an array of column names for the scope (as shown above in the description)... if I have a chance this weekend, I'll check about making an official patch.

def validates_uniqueness_of(*attr_names)

configuration = { :message => ActiveRecord::Errors.default_error_messages[:taken] } configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash) if scope = configuration[:scope]

validates_each(attr_names,configuration) do |record, attr_name, value|

if scope.is_a?(Array)

full_scope = "" scope.each do |s|

full_scope += sanitize_sql(["#{s} = ?", record.send(s)]) full_scope += " AND " if s != scope.last

end

else

full_scope = sanitize_sql(["#{scope} = ?", record.send(scope)])

end

record.errors.add(attr_name, configuration[:message]) if record.class.find_first(record.new_record? ? ["#{attr_name} = ? AND #{full_scope}", record.send(attr_name)] : ["#{attr_name} = ? AND #{record.class.primary_key} <> ? AND #{full_scope}", record.send(attr_name), record.send(:id)])

end

else

validates_each(attr_names,configuration) do |record, attr_name, value|

record.errors.add(attr_name, configuration[:message]) if record.class.find_first(record.new_record? ? ["#{attr_name} = ?", record.send(attr_name)] : ["#{attr_name} = ? AND #{record.class.primary_key} <> ?", record.send(attr_name), record.send(:id) ] )

end

end

end

06/30/05 18:12:47 changed by anonymous

Whoa! Sorry about that formatting... I just attached a file: validates_uniqueness_of.rb.

06/30/05 18:13:19 changed by anonymous

  • attachment validates_uniqueness_of.rb added.

07/17/05 14:44:18 changed by anonymous

  • summary changed from validates_uniqueness_of scope request. to [XPATCH] validates_uniqueness_of scope request..

07/18/05 04:01:26 changed by anonymous

  • milestone set to 1.x.

Setting all XPATCHs out to 1.x

12/01/05 19:51:44 changed by jeremy@jthopple.com

  • attachment array_scope_for_validates_uniqueness_of.diff added.

I finally got around

12/01/05 19:54:30 changed by jeremy@jthopple.com

I finally got around to putting together a patch for this request. It includes the actual change to validates_uniqueness_of, documentation updates, and a new unit test.

12/01/05 19:59:01 changed by anonymous

  • summary changed from [XPATCH] validates_uniqueness_of scope request. to [PATCH] validates_uniqueness_of scope request..

12/01/05 20:11:42 changed by jeremy@jthopple.com

  • attachment array_scope_for_validates_uniqueness_of.2.diff added.

Fixed some typos in my comments. Ignore the first diff.

12/01/05 20:16:13 changed by jeremy@jthopple.com

  • attachment array_scope_for_validates_uniqueness_of.3.diff added.

One more!!! My local connection changes are mistakenly in the second one.

12/02/05 04:30:49 changed by marcel

  • status changed from new to closed.
  • resolution set to fixed.

(In [3206]) Allow validate_uniqueness_of to be scoped by more than just one column. Closes #1559.

12/02/05 04:31:32 changed by marcel

Thanks for including comprehensive tests.