validates-uniqueness-of

Rails - Validate Nested Attributes Uniqueness with scope parent of parent

好久不见. 提交于 2019-12-03 10:17:22
问题 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, dependent: :destroy end #app/models/contact.rb class Contact < ActiveRecord::Base belongs_to :account has_many :email_addresses, dependent: :destroy, validate: :true, inverse_of: :contact accepts_nested_attributes_for :email_addresses,allow_destroy:

Rails 3: Validate combined values

二次信任 提交于 2019-12-03 03:01:17
问题 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 => true This would make sure the husband/wife combination is unique in the database. Now, in Rails 3 the validation syntax changed and the scope attribute seems to be gone. It now looks like: validates :husband, :presence => true Any idea how I can

Rails 3: Validate combined values

六眼飞鱼酱① 提交于 2019-12-02 16:34:45
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 => true This would make sure the husband/wife combination is unique in the database. Now, in Rails 3 the validation syntax changed and the scope attribute seems to be gone. It now looks like: validates :husband, :presence => true Any idea how I can achieve the combined validation in Rails 3? The Rails 2.x validations still work in Rails 3 so I can

Rails 3: Uniqueness validation for nested fields_for

北城以北 提交于 2019-11-30 05:10:02
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 try to enter the same name for a new product, the uniqueness validation triggers successfully. However, if I enter the same product name in 2 rows of the same nested form, the form is accepted - the uniqueness validation doesn't trigger. I'm guessing this is a fairly common problem, but I can't find any simple solution. Anyone have any suggestions

Rails 3: Uniqueness validation for nested fields_for

纵饮孤独 提交于 2019-11-29 02:59:31
问题 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 try to enter the same name for a new product, the uniqueness validation triggers successfully. However, if I enter the same product name in 2 rows of the same nested form, the form is accepted - the uniqueness validation doesn't trigger. I'm

validates_uniqueness_of in destroyed nested model rails

不羁的心 提交于 2019-11-27 17:45:12
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 :name end Uniqueness validation in Task model gives problem while updating Project. In edit of project i delete a task T1 and then add a new task with same name T1, uniqueness validation restricts the saving of Project. params hash look something like task_attributes => { {"id" => "1","name" => "T1", "_destroy" => "1"},{"name" => "T1"}} Validation on task is

Rails validate uniqueness only if conditional

风流意气都作罢 提交于 2019-11-27 13:29:37
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 to achieve that elegantly? You can make a validation conditional by passing either a simple string of

validates_uniqueness_of in destroyed nested model rails

不羁的心 提交于 2019-11-26 19:09:43
问题 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 :name end Uniqueness validation in Task model gives problem while updating Project. In edit of project i delete a task T1 and then add a new task with same name T1, uniqueness validation restricts the saving of Project. params hash look something like task