Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4 validate uniqueness with hash scope deprecated

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 can't figure out the new syntax. I want to validate both presence and uniqueness for a data field.

like image 887
markhorrocks Avatar asked Jun 07 '13 10:06

markhorrocks


1 Answers

how about this ?

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

See The Rails Guides for more info. Your syntax dates from rails 2 !

EDIT

You can now also use the allow_blank option instead of a presence validation, which makes for nicer error messages :

validates :sport_name, uniqueness: {scope: :sports_org_id, allow_blank: false}
like image 130
m_x Avatar answered Nov 07 '22 11:11

m_x