Rails 4 Confirms Uniqueness with Hash Memory Obsolete

In Rails 3.2, I have this syntax:

validates_uniqueness_of :sport_name, :scope => :sports_org_id 

This is now deprecated in rails 4, but I cannot understand the new syntax. I want to check the availability and uniqueness of a data field.

+8
ruby-on-rails
source share
1 answer

how about this?

 validates :sport_name, uniqueness: {scope: :sports_org_id}, presence: true 

For more information, see The Rails Guides . Your syntax dates from rails 2!

EDIT

Now you can also use the allow_blank option instead of checking for presence, which makes error messages more enjoyable:

 validates :sport_name, uniqueness: {scope: :sports_org_id, allow_blank: false} 
+19
source share

All Articles