ActiveRecord的唯一性验证器有一个选项,如果值为nil或空白,则跳过验证.即使我将两个参数都设置为true(默认行为),我也可以在验证命中之前创建一个nil和空白的记录.我使用默认的SQlite3数据库sqlite3-ruby(1.2.5).
编辑以澄清:如果我添加validates_presence_of
到模型,我会得到预期的结果.我认为默认行为validates_uniqueness_of
会使这个多余.
测试用例:
rails validation_test
cd validation_test/
script/generate Model Thing identification:string
rake db:migrate
Run Code Online (Sandbox Code Playgroud)
app/models/thing.rb的内容:
class Thing < ActiveRecord::Base
validates_uniqueness_of :identification
end
Run Code Online (Sandbox Code Playgroud)
Rails控制台:
script/console
Loading development environment (Rails 2.3.4)
>> Thing.create!
=> #<Thing id: 1, identification: nil, created_at: "2009-09-26 01:49:32", updated_at: "2009-09-26 01:49:32">
>> Thing.create! :identification => ""
=> #<Thing id: 2, identification: "", created_at: "2009-09-26 01:49:42", updated_at: "2009-09-26 01:49:42">
>> Thing.create! :identification => ""
ActiveRecord::RecordInvalid: Validation failed: Identification has already been taken
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.4/lib/active_record/validations.rb:1090:in `save_without_dirty!' …
Run Code Online (Sandbox Code Playgroud) 在Rails 2.x中,您可以使用验证来确保您具有如下所示的唯一组合值:
validates_uniqueness_of :husband, :scope => :wife
Run Code Online (Sandbox Code Playgroud)
在相应的迁移中,它可能如下所示:
add_index :family, [:husband, :wife], :unique => true
Run Code Online (Sandbox Code Playgroud)
这将确保丈夫/妻子组合在数据库中是唯一的.现在,在Rails 3中,验证语法发生了变化,范围属性似乎消失了.它现在看起来像:
validates :husband, :presence => true
Run Code Online (Sandbox Code Playgroud)
知道如何在Rails 3中实现组合验证吗?Rails 2.x验证仍然可以在Rails 3中使用,所以我仍然可以使用第一个示例,但它看起来很"旧",有更好的方法吗?
validation ruby-on-rails database-integrity validates-uniqueness-of ruby-on-rails-3
我有一个问题类:
class Question < ActiveRecord::Base
attr_accessible :user_id, :created_on
validates_uniqueness_of :created_on, :scope => :user_id
end
Run Code Online (Sandbox Code Playgroud)
给定用户每天只能创建一个问题,因此我希望通过唯一索引强制数据库中的唯一性,并通过validates_uniqueness_of强制使用Question类.
我遇到的麻烦是我只想要非管理员用户的约束.因此,管理员可以根据需要每天创建尽可能多的问题.如何实现优雅的任何想法?
我有一个Project模型,它接受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
Run Code Online (Sandbox Code Playgroud)
任务模型中的唯一性验证在更新Project时会出现问题.
在编辑项目时,我删除任务T1,然后添加一个同名T1的新任务,唯一性验证限制了项目的保存.
params hash看起来像
task_attributes => { {"id" =>
"1","name" => "T1", "_destroy" =>
"1"},{"name" => "T1"}}
Run Code Online (Sandbox Code Playgroud)
在销毁旧任务之前完成对任务的验证.因此验证失败.任何想法如何验证,它不会考虑任务被销毁?
我的迁移文件中有以下内容
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
Run Code Online (Sandbox Code Playgroud)
我想确保如果指定了product_id它是唯一的但我也想允许null所以我在我的模型中有以下内容:
validates :product_id,
:uniqueness => true,
:allow_nil => true
Run Code Online (Sandbox Code Playgroud)
工作得很好,但我应该为迁移文件添加一个索引
add_index :payment_agreements, :product_id, :unique => true
Run Code Online (Sandbox Code Playgroud)
显然,当为product_id插入两个空值时,这将抛出异常.我可以简单地省略迁移中的索引,但是我有可能获得两个PaymentAgreements,其中product_id与此处所示相同:并发性和完整性
我的问题是处理这个问题的最佳/最常用方法是什么
migration ruby-on-rails unique-index validates-uniqueness-of
我有一个有两个字段的模型,我将其称为first_name和last_name,我想确保两者的组合不区分大小写.我已经在那里使用了这个:
validates_uniqueness_of :first_name, :scope => :last_name
Run Code Online (Sandbox Code Playgroud)
问题是,唯一性检查似乎区分大小写,即使文档说默认情况下它应该不区分大小写.所以给出一个现有记录:
{ :first_name => 'John', :last_name => 'Smith' }
Run Code Online (Sandbox Code Playgroud)
这将是允许的:
{ :first_name => 'JOHN', :last_name => 'SMITH' }
Run Code Online (Sandbox Code Playgroud)
以及在名字或名字中有任何案例变异的任何其他记录.为什么允许这些记录?如何在两个字段中一起强制使用不区分大小写的唯一性?
scope ruby-on-rails case-insensitive validates-uniqueness-of
A有两个型号,"商店"和"产品",通过has_many:through链接.
在商店表单中有多个产品的嵌套属性,我在产品的唯一性验证方面遇到了一些麻烦.如果我输入产品,保存它,然后尝试为新产品输入相同的名称,则唯一性验证会成功触发.
但是,如果我在同一嵌套表单的两行中输入相同的产品名称,则表单被接受 - 唯一性验证不会触发.
我猜这是一个相当普遍的问题,但我找不到任何简单的解决方案.任何人都有最简单的方法建议,以确保在同一嵌套表格中遵守唯一性验证?
编辑:下面包含的产品型号
class Product < ActiveRecord::Base
has_many :shop_products
has_many :shops, :through => :shop_products
validates_presence_of :name
validates_uniqueness_of :name
end
Run Code Online (Sandbox Code Playgroud) 我有以下模型,这些模型基本上试图表示教授对特定级别的许多科目有所了解.主题是固定的,因此不会创建新的主题,通过知识连接表只会与教授"相关".
class Subject < ActiveRecord::Base
# Self Associations
has_many :subcategories, :class_name => "Subject"
belongs_to :category, :class_name => "Subject",:foreign_key => "parent_id"
# Associations
has_many :knowledges
has_many :professors, :through => :knowledges
end
class Professor < ActiveRecord::Base
# Associations
has_many :knowledges
has_many :subjects, :through => :knowledges
...
end
class Knowledge < ActiveRecord::Base
# Associations
belongs_to :professor
belongs_to :subject
has_one :level
attr_accessible :subject_id, :professor_id
validates :subject_id, :uniqueness => { :scope => :professor_id }
end
Run Code Online (Sandbox Code Playgroud)
我希望有一个表格可以让教授在他的帐户中添加一个主题,我决定有一个知识形式(因为我希望能够插入一个级别).
它看起来像这样: …
这是无效的代码
class WeekDay < ActiveRecord::Base
validates_inclusion_of :day, :in => %w(sunday monday tuesday wednesday thursday friday saturday), :case_sensitive => false
end
Run Code Online (Sandbox Code Playgroud)
目前我在数据库中的所有日子除了星期日.我试图添加"星期日",并获得错误"不包括在列表中".
validation model ruby-on-rails case-sensitive validates-uniqueness-of
我有以下(消毒过的)模型:
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
Run Code Online (Sandbox Code Playgroud)
问题如下,
让我们说人Joe Shmoe目前有两个地址附在自己身上
666 Foo Street,12345,唯一ID:"ABCDEFG"和777 Lucky Avenue,54321,唯一ID:"GFEDCBA"
并且让我们说以下帖子来自一个表单:
{:addresses_attributes =>
{ [0] => {:unique_per_person_government_id => “ABCDEFG”, :street=> “Foo Street”, :house_number => 666, :zip_code=>12345, _destroy => 1}
[1] => {:unique_per_person_government_id => “ABCDEFG”, :street=>”Bar Street”, :house_number => 888, :zip_code => …
Run Code Online (Sandbox Code Playgroud) ruby validation ruby-on-rails validates-uniqueness-of rails-activerecord
validation ×3
model ×2
activerecord ×1
jointable ×1
migration ×1
nested-forms ×1
ruby ×1
scope ×1
unique-index ×1