Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby Rails 3.0.9:ActiveRecord唯一性约束在每次更新时都会失败,不会';唯一列是否为';不碰_Ruby_Ruby On Rails 3_Activerecord - Fatal编程技术网

Ruby Rails 3.0.9:ActiveRecord唯一性约束在每次更新时都会失败,不会';唯一列是否为';不碰

Ruby Rails 3.0.9:ActiveRecord唯一性约束在每次更新时都会失败,不会';唯一列是否为';不碰,ruby,ruby-on-rails-3,activerecord,Ruby,Ruby On Rails 3,Activerecord,我有一个侧面模型 class Profile < ActiveRecord::Base attr_accessible :user_id, :race_id, :nickname, :first_name, :last_name, :gender, :birth_date, :eighteen, :time_zone, :metric_scale, :referral_code, :referrer_id, :tag_line # Relationsh

我有一个侧面模型

class Profile < ActiveRecord::Base
    attr_accessible :user_id, :race_id, :nickname, :first_name, :last_name, :gender, :birth_date, :eighteen,
            :time_zone, :metric_scale, :referral_code, :referrer_id, :tag_line

  # Relationships
  belongs_to  :user
  belongs_to  :race
  belongs_to :referred_by, :class_name => "Profile", :foreign_key => "referral_code"
  has_many :referrals, :class_name => "Profile", :foreign_key => "referrer_id"

  # Validations
  validates :user_id, :race_id, :nickname, :first_name, :last_name, :time_zone, :gender, :presence => true
  validates :referral_code, :nickname, :uniqueness => { :case_sensitive => false }

  # Instance Methods
  def full_name
    first_name + " " + last_name
  end

  # Class Methods
  def self.search(search)
    search_condition = "%" + search + "%"
    find(:all, :conditions => ['nickname LIKE ?', search_condition])
  end

  def self.find_by_referral_code(referrer_code)
    find(:one, :conditions => ['referral_code LIKE ?', referrer_code])
  end

end
类配置文件“配置文件”;:外键=>“引用代码”
有很多:推荐人,:class\u name=>“Profile”,:foreign\u key=>“推荐人\u id”
#验证
验证:user\u id、:race\u id、:昵称、:first\u name、:last\u name、:time\u zone、:gender、:presence=>true
验证:引用\代码,:昵称,:唯一性=>{:区分大小写=>false}
#实例方法
def全名
名+姓+姓
结束
#类方法
def self.search(搜索)
搜索条件=“%”+搜索+“%”
查找(:all,:conditions=>['昵称类似?',搜索条件])
结束
定义自我。通过推荐代码查找推荐代码(推荐代码)
查找(:one,:conditions=>['reference\u code LIKE?',referer\u code])
结束
结束
无论我更新了哪一列,“referral_code”上的唯一性约束为false,我都无法更新模型,也无法找出原因。从我在线阅读的Rails 3来看,ActiveRecord应该是跟踪脏对象,只生成包含修改列的更新查询,而不生成其他列。因为它应该只对唯一列以外的列执行更新查询,所以验证不应该失败。不幸的是。以下是Rails控制台会话,其中显示:

Loading development environment (Rails 3.0.9)
ruby-1.9.2-p180 :001 > profile = Profile.find(3)
 => #<Profile id: 3, user_id: 3, race_id: 2, nickname: "Premium-User", first_name: "Premium", last_name: "User", gender: "M", birth_date: "1977-01-01", eighteen: true, complete: true, time_zone: "Kuala Lumpur", metric_scale: false, referral_code: "bo", referrer_id: nil, tag_line: "This is what its like.", created_at: "2011-09-21 04:08:00", updated_at: "2011-09-21 04:08:00"> 

ruby-1.9.2-p180 :002 > update = {"tag_line"=>"Changed to this"}
 => {"tag_line"=>"Changed to this"} 

ruby-1.9.2-p180 :003 > profile.update_attributes(update)
 => false 

ruby-1.9.2-p180 :004 > profile.errors
 => {:referral_code=>["has already been taken"]}
加载开发环境(Rails 3.0.9)
ruby-1.9.2-p180:001>profile=profile.find(3)
=> # 
ruby-1.9.2-p180:002>更新={“tag_line”=>“更改为此”}
=>{“标记线”=>“更改为此”}
ruby-1.9.2-p180:003>配置文件。更新属性(更新)
=>错误
ruby-1.9.2-p180:004>profile.errors
=>{:referral_code=>[“已采取”]}
即使直接在非唯一的单个列上执行更新,也会导致唯一性约束失败,并且记录不会被更新,下面是一个控制台会话:

Loading development environment (Rails 3.0.9)
ruby-1.9.2-p180 :001 > profile = Profile.find(3)
 => #<Profile id: 3, user_id: 3, race_id: 2, nickname: "Premium-User", first_name: "Premium", last_name: "User", gender: "M", birth_date: "1977-01-01", eighteen: true, complete: true, time_zone: "Kuala Lumpur", metric_scale: false, referral_code: "bo", referrer_id: nil, tag_line: "This is what its like.", created_at: "2011-09-21 04:08:00", updated_at: "2011-09-21 04:08:00"> 

ruby-1.9.2-p180 :002 > profile.tag_line = "changed to this"
 => "changed to this" 

ruby-1.9.2-p180 :003 > profile.save
 => false 

ruby-1.9.2-p180 :004 > profile.errors
 => {:referral_code=>["has already been taken"]}
Loading development environment (Rails 3.0.9)
ruby-1.9.2-p180 :001 > profile = Profile.find(3)
 => #<Profile id: 3, user_id: 3, race_id: 2, nickname: "Premium-User", first_name: "Premium", last_name: "User", gender: "M", birth_date: "1977-01-01", eighteen: true, complete: true, time_zone: "Kuala Lumpur", metric_scale: false, referral_code: "bo", referrer_id: nil, tag_line: "This is what its like.", created_at: "2011-09-21 04:08:00", updated_at: "2011-09-21 04:08:00"> 

ruby-1.9.2-p180 :002 > profile.tag_line = "change to this"
 => "change to this" 

ruby-1.9.2-p180 :003 > profile.changes
 => {"tag_line"=>["This is what its like.", "change to this"]} 

ruby-1.9.2-p180 :004 > profile.save
 => false 

ruby-1.9.2-p180 :005 > profile.errors
 => {:referral_code=>["has already been taken"]}
加载开发环境(Rails 3.0.9)
ruby-1.9.2-p180:001>profile=profile.find(3)
=> # 
ruby-1.9.2-p180:002>profile.tag\u line=“更改为此”
=>“已更改为此”
ruby-1.9.2-p180:003>profile.save
=>错误
ruby-1.9.2-p180:004>profile.errors
=>{:referral_code=>[“已采取”]}
我还运行了一个检查,查看ActiveRecord是否确实在跟踪脏对象,它似乎是这样的,下面是控制台会话:

Loading development environment (Rails 3.0.9)
ruby-1.9.2-p180 :001 > profile = Profile.find(3)
 => #<Profile id: 3, user_id: 3, race_id: 2, nickname: "Premium-User", first_name: "Premium", last_name: "User", gender: "M", birth_date: "1977-01-01", eighteen: true, complete: true, time_zone: "Kuala Lumpur", metric_scale: false, referral_code: "bo", referrer_id: nil, tag_line: "This is what its like.", created_at: "2011-09-21 04:08:00", updated_at: "2011-09-21 04:08:00"> 

ruby-1.9.2-p180 :002 > profile.tag_line = "changed to this"
 => "changed to this" 

ruby-1.9.2-p180 :003 > profile.save
 => false 

ruby-1.9.2-p180 :004 > profile.errors
 => {:referral_code=>["has already been taken"]}
Loading development environment (Rails 3.0.9)
ruby-1.9.2-p180 :001 > profile = Profile.find(3)
 => #<Profile id: 3, user_id: 3, race_id: 2, nickname: "Premium-User", first_name: "Premium", last_name: "User", gender: "M", birth_date: "1977-01-01", eighteen: true, complete: true, time_zone: "Kuala Lumpur", metric_scale: false, referral_code: "bo", referrer_id: nil, tag_line: "This is what its like.", created_at: "2011-09-21 04:08:00", updated_at: "2011-09-21 04:08:00"> 

ruby-1.9.2-p180 :002 > profile.tag_line = "change to this"
 => "change to this" 

ruby-1.9.2-p180 :003 > profile.changes
 => {"tag_line"=>["This is what its like.", "change to this"]} 

ruby-1.9.2-p180 :004 > profile.save
 => false 

ruby-1.9.2-p180 :005 > profile.errors
 => {:referral_code=>["has already been taken"]}
加载开发环境(Rails 3.0.9)
ruby-1.9.2-p180:001>profile=profile.find(3)
=> # 
ruby-1.9.2-p180:002>profile.tag\u line=“更改为此”
=>“更改为此”
ruby-1.9.2-p180:003>profile.changes
=>{“tag_line”=>[“这就是它的样子。”,“改成这个”]}
ruby-1.9.2-p180:004>profile.save
=>错误
ruby-1.9.2-p180:005>profile.errors
=>{:referral_code=>[“已采取”]}

老实说,我真是不知所措,我花了相当多的时间来研究它,也搜索了谷歌,但我找不到一个答案来解释为什么会发生这种情况。

你是对的,Rails只“跟踪”脏列,并生成所需的最小更新语句。如果查看log/development.log文件,您将看到正在生成的实际SQL,并且您将看到update语句只涉及您编辑的列。如果你的代码有那么大的进步,至少你会这么做

在保存模型之前,Rails将在模型上运行所有验证,其中包括查看引用代码是否唯一。为此,它将对要检查的数据库运行SELECTSQL语句;如果查看development.log文件,您肯定会看到此查询

所以Rails在这里工作正常


如果你的推荐代码应该是唯一的,为什么不呢?我的猜测是,您试图用零或空代码保存模型。如果是这种情况,请尝试将
:allow\u nil=>true
:allow\u blank=>true
添加到:university散列中。

您从概要文件中得到了什么。按引用代码查找所有内容(Profile.reference\u code)?此约束是否也在数据库级别实现?转储该表的DDL并检查是否存在任何键/约束。还要检查log/development.log以获取更新正在运行的实际SQL查询。