validates-uniqueness-of

Reservation Type System w/ Validation - Rails 3.0

半城伤御伤魂 提交于 2020-01-15 08:28:27
问题 What would be the best approach to trying to "validate" uniqueness of dates for object. -- So for example, I have a Room, that can only have 1 person in it at a time. A User can book this Room in advance. Currently the Room is booked from the 1st of March to the 5th of March. I want to prevent anyone from booking again on those dates. So if someone attempts to book a room from the 25th of Feb to the 2nd of March, I need to tell them they cannot, because the room is fully booked. -- I am using

Rails validates_uniqueness_of across multiple columns with case insensitivity

谁都会走 提交于 2019-12-21 03:33:11
问题 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, :scope => :last_name The problem is that the uniqueness check seems to be case sensitive, even though the documentation says it should be case insensitive by default. So given an existing record: { :first_name => 'John', :last_name => 'Smith' } This

Rails validate uniqueness only if conditional

人走茶凉 提交于 2019-12-17 10:52:39
问题 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 uniqueness in the database via a unique index and the Question class via validates_uniqueness_of. The trouble I'm running into is that I only want that constraint for non-admin users. So admins can create as many questions per day as they want. Any ideas for how

Core Data uniqueness

纵饮孤独 提交于 2019-12-11 13:14:38
问题 Is there any way I can validate a value updated in a Core Data entity's property against values of the property in other entities in the collection? At the moment I create an entity with some default values, add it to arrangedObjects , then get the user to modify the various property values. However, I would like to check a particular property and make sure there're no other entities in the array with the same value for that property. What's the best way to do this? Many thanks, Dany. 回答1:

Mongoid 3 - Check for uniqueness of composite key

感情迁移 提交于 2019-12-10 21:16:24
问题 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 => String field :password, :type => String validates_presence_of :ip validates_presence_of :port end How to get a validates_uniqueness_of therein which should check if ip and port are unique as composite field? AFAIK there was a way in Mongoid 2 to create a new

Rails uniqueness constraint and matching db unique index for null column

岁酱吖の 提交于 2019-12-09 14:18:39
问题 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 t.references :seller t.references :product t.timestamps end end I want to ensure that if a product_id is specified it is unique but I also want to allow null so I have the following in my model: validates :product_id, :uniqueness => true, :allow_nil => true Works great but I should then

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

大憨熊 提交于 2019-12-07 04:42:44
问题 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: :person accepts_nested_attributes_for :addresses, allow_destroy: true end class Address < ActiveRecord::Base attr_accessible :zip_code, :street,:house_number, :unique_per_person_government_id belongs_to :person, inverse_of: :addresses validates_uniqueness_of

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

二次信任 提交于 2019-12-05 10:31:34
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: :person accepts_nested_attributes_for :addresses, allow_destroy: true end class Address < ActiveRecord::Base attr_accessible :zip_code, :street,:house_number, :unique_per_person_government_id belongs_to :person, inverse_of: :addresses validates_uniqueness_of :unique_per_person_government_id, scope: :person_id end The problem is as follows, Lets say Person Joe

Rails uniqueness constraint and matching db unique index for null column

那年仲夏 提交于 2019-12-03 23:03:14
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 t.references :seller t.references :product t.timestamps end end I want to ensure that if a product_id is specified it is unique but I also want to allow null so I have the following in my model: validates :product_id, :uniqueness => true, :allow_nil => true Works great but I should then add an index to the migration file add_index :payment_agreements, :product_id, :unique => true Obviously

Rails validates_uniqueness_of across multiple columns with case insensitivity

扶醉桌前 提交于 2019-12-03 11:41:23
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, :scope => :last_name The problem is that the uniqueness check seems to be case sensitive, even though the documentation says it should be case insensitive by default. So given an existing record: { :first_name => 'John', :last_name => 'Smith' } This will be allowed: { :first_name => 'JOHN', :last_name => 'SMITH' } As well as any additional record where