这是我从旧数据库移动数据的代码:
class Old < ActiveRecord::Base
establish_connection :old_version
self.abstract_class = true
class Recipe < self
set_table_name :recipes
has_many :uploaded_files, :as => :storage
end
class UploadedFile < self
set_table_name :uploaded_files
belongs_to :storage, :polymorphic => true
end
end
当我运行以下代码时
Old::Recipe.all.each do |recipe|
puts recipe.uploaded_files.to_sql
end
执行此SQL
SELECT `uploaded_files`.* FROM `uploaded_files` WHERE `uploaded_files`.`storage_id` = 38 AND `uploaded_files`.`storage_type` = 'Old::Recipe'
问题是我得到了:
`storage_type` = 'Old::Recipe'
但我需要:
`storage_type` = 'Recipe'
如何更改多态关系的类?
has_many
的文档没有给我答案。
答案 0 :(得分:3)
最近我遇到了类似的问题,这是一个适用于我在rails 4.2中的解决方案:
class Recipe < self
set_table_name :recipes
has_many :old_files, -> (object) { unscope(where: :storage_type).where(storage_type: 'Recipe') }, class_name: 'UploadedFile'
end
您必须添加unscope(:where)
才能从查询中删除条件uploaded_files.storage_type = 'Old::Recipe'
。
答案 1 :(得分:2)
上面santuxus的答案在4.2+的滑轨上正常工作
但是,对于较低版本,您可以尝试像这样覆盖关联:
class Recipe
has_many :uploaded_files, conditions: { storage_type: 'Recipe' },
foreign_key: :storage_id
end
答案 2 :(得分:0)
不幸的是,现在我发现只有一种方法可以做到这一点:
class Old < ActiveRecord::Base
establish_connection :old_version
self.abstract_class = true
class Recipe < self
set_table_name :recipes
has_many :old_files,
:class_name => 'UploadedFile',
:finder_sql => Proc.new {
%Q{
SELECT uploaded_files.*
FROM uploaded_files
WHERE uploaded_files.storage_id = #{id} AND
uploaded_files.storage_type = 'Recipe'
}
}
end
class UploadedFile < self
set_table_name :uploaded_files
belongs_to :storage, :polymorphic => true
end
end
namespace :old do
task :menu => :environment do
Old::Recipe.all.each do |recipe|
puts '~' * 50
puts recipe.id
recipe.old_files.to_a.each do |file|
puts file.storage_id, file.storage_type
end
end
end
end
非常非常伤心