Questions tagged [validates-uniqueness-of]

validates_uniqueness_of is a rails helper method for determining whether an attribute is unique throughout a system

In Ruby on Rails, the validates_uniqueness_of method determines whether an attribute is unique across a system.

http://apidock.com/rails/v4.2.1/ActiveRecord/Validations/ClassMethods/validates_uniqueness_of

90 questions
67
votes
2 answers

validates_uniqueness_of passes on nil or blank (without allow_nil and allow_blank)

The uniqueness validator of ActiveRecord has an options to skip validation if the value is nil or blank. Even if I set both parameters to true (the default behaviour) I can create one record with nil and blank before the validation hits. I use the…
Roman
  • 339
  • 1
  • 5
  • 14
40
votes
1 answer

Rails 3: Validate combined values

In Rails 2.x you can use validations to make sure you have a unique combined value like this: validates_uniqueness_of :husband, :scope => :wife In the corresponding migration it could look like this: add_index :family, [:husband, :wife], :unique =>…
33
votes
3 answers

Rails validate uniqueness only if conditional

I have a Question class: class Question < ActiveRecord::Base attr_accessible :user_id, :created_on validates_uniqueness_of :created_on, :scope => :user_id end A given user can only create a single question per day, so I want to force…
kid_drew
  • 3,445
  • 6
  • 25
  • 34
19
votes
5 answers

validates_uniqueness_of in destroyed nested model rails

I have a Project model which accepts nested attributes for Task. class Project < ActiveRecord::Base has_many :tasks accepts_nested_attributes_for :tasks, :allow_destroy => :true end class Task < ActiveRecord::Base validates_uniqueness_of…
arun
  • 303
  • 2
  • 6
16
votes
2 answers

Rails uniqueness constraint and matching db unique index for null column

I have the following in my migration file def self.up create_table :payment_agreements do |t| t.boolean :automatic, :default => true, :null => false t.string :payment_trigger_on_order t.references :supplier …
14
votes
1 answer

Rails validates_uniqueness_of across multiple columns with case insensitivity

I have a model that has two fields, which I will call first_name and last_name, and I want to make sure that the combination of the two are case-insensitively unique. I've gotten halfway there by using this: validates_uniqueness_of :first_name,…
Jimmy
  • 31,408
  • 11
  • 74
  • 93
10
votes
3 answers

Rails 3: Uniqueness validation for nested fields_for

A have two models, "shop" and "product", linked via has_many :through. In the shop form there are nested attributes for multiple products, and I'm having a little trouble with the product's uniqueness validation. If I enter a product, save it, then…
PlankTon
  • 11,872
  • 14
  • 82
  • 144
7
votes
2 answers

Rails: Uniqueness of two attributes in join table causing 500 error

I have the following models, which basically are trying to mean that a professor has knowledge of many subjects for a particular level. The subjects are fixed, so there will be no new subjects created, there will be just "related" to a professor…
Nobita
  • 22,569
  • 10
  • 55
  • 85
5
votes
3 answers

Rails Model Validation: i need validates_inclusion_of with case sensitive false?

Here is code which is not working class WeekDay < ActiveRecord::Base validates_inclusion_of :day, :in => %w(sunday monday tuesday wednesday thursday friday saturday), :case_sensitive => false end Currently i have all of days in…
5
votes
1 answer

rails validation of nested attributes for uniqueness when some may be marked for destroy

I've got the following (sanitized) models: class Person < ActiveRecord::Base attr_accessible :name, :first_name, :last_name, :age, :job_title, :salary, :ssn, :prison_convictions, :addresses_attributes has_many :addresses, inverse_of:…
4
votes
2 answers

Rails - Validate Nested Attributes Uniqueness with scope parent of parent

I have a problem with the scoped uniqueness validation in Rails for nested attributes with a parent of parent. Background I have a rails 4 application with 3 models : #app/models/account.rb class Account < ActiveRecord::Base has_many :contacts,…
3
votes
1 answer

Rails model: validates_uniqueness_of doesn't remove trailing spaces not leading ones before unique check?

Suppose i implement validates_uniqueness_of on name of user. If name 'maddy' already exists then it will accept value ' maddy' as unique value but not 'maddy '. It should remove spaces both sides. How to have that behaviour?
Maddy.Shik
  • 6,241
  • 15
  • 62
  • 98
3
votes
1 answer

Rails validates_uniqueness_of :scope on foreign key

I have a model "Product" that belongs_to "Store" (which has_many "products"). I want to validate the uniqueness of the product name but only within each store. Right now I have this: class Product < ActiveRecord::Base belongs_to :store …
lightyrs
  • 2,620
  • 2
  • 26
  • 31
3
votes
1 answer

Mongoid 3 - Check for uniqueness of composite key

I switched to Mongoid 3 which makes few things different :) Currently I try to check if a composite field is unique: class Host include Mongoid::Document field :ip, :type => String field :port, :type => Integer field :username, :type =>…
ctp
  • 1,025
  • 1
  • 7
  • 27
2
votes
1 answer

validates_uniqueness_of before ActiveRecord::RecordNotUnique

I have the following class in a Rails 3.1.1 app: class User < ActiveRecord::Base attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :admin, :moderator, :bio validates_presence_of :username …
DubaiCoder
  • 58
  • 1
  • 5
1
2 3 4 5 6