Ruby on rails 修复多态关联的不一致性
我的模型中有一个多态关联,它在创建时遇到了一个间歇性的bug(在生产中)。我修复了它,但是现在我需要修复在这个bug下创建的条目 我的模型:Ruby on rails 修复多态关联的不一致性,ruby-on-rails,activerecord,polymorphic-associations,Ruby On Rails,Activerecord,Polymorphic Associations,我的模型中有一个多态关联,它在创建时遇到了一个间歇性的bug(在生产中)。我修复了它,但是现在我需要修复在这个bug下创建的条目 我的模型: class User < ActiveRecord::Base belongs_to :userable, :polymorphic => true attr_protected :userable end class UserType1 < ActiveRecord::Base has_one :user, :as =&g
class User < ActiveRecord::Base
belongs_to :userable, :polymorphic => true
attr_protected :userable
end
class UserType1 < ActiveRecord::Base
has_one :user, :as => :userable, :dependent => :destroy
end
class UserType2 < ActiveRecord::Base
has_one :user, :as => :userable, :dependent => :destroy
end
而UserType1没有任何相关的条目
所以我做了一个查询来检测所有输入错误:
SELECT users.* from users where COALESCE(userable_id, 0) = 0'
但我不知道如何把它修好
我的想法是创建这样一个脚本:
User.find_by_sql('SELECT users.* from users where COALESCE(userable_id, 0) = 0').each do |user|
userable = user.userable_type.classify.constantize.new
userable.user = user
userable.save
user.userable = userable
user.save
end
谢谢为什么要构建并保存一个可使用的?关于零问题的方法?建议一个标题:“如何编写rake任务来操作数据库”
User.find_by_sql('SELECT users.* from users where COALESCE(userable_id, 0) = 0').each do |user|
userable = user.userable_type.classify.constantize.new
userable.user = user
userable.save
user.userable = userable
user.save
end
class User
alias_method :old_userable, :userable
def userable
old_userable || Userable.new
end
end