我有以下三个模型
LegacyRole:
class LegacyRole < LegacyModel
has_many :permissions_roles
has_many :permissions, :through => :permissions_roles
end
LegacyPermissionsRole:
class LegacyPermissionsRole < LegacyModel
belongs_to :role
belongs_to :permission
end
和LegacyPermission:
class LegacyPermission < LegacyModel
has_many :permissions_roles
has_many :roles, :through => :permissions_roles
end
为了使这些工作全部工作,并连接遗留数据库和诸如此类的东西,我有以下类LegacyModel可能试图太聪明......
require 'active_record'
class LegacyModel < ActiveRecord::Base
self.abstract_class = true
establish_connection "legacy_#{::Rails.env}"
def self.inherited(subclass)
tabeleized_name = subclass.name.tableize
raise "Legacy models must be prefixed with 'Legacy'" unless tabeleized_name.start_with?('legacy_')
logger.info "***********LOAD***********"
logger.info "Loaded legacy model: #{subclass.name} using table: #{tabeleized_name.gsub('legacy_', '')}"
super
subclass.set_table_name tabeleized_name.gsub('legacy_','')
end
# these methods do much the same thing, can probably abstract some of this out
def self.belongs_to(association_id, options = {})
new_association = association_id.to_s.insert(0, 'legacy_').to_sym
old_association = association_id
logger.info "Legacy model has belongs_to association: '#{association_id}'"
association_id = association_id.to_s.insert(0, 'legacy_').to_sym
logger.info "Converting association to: '#{association_id}'"
unless options.has_key?(:foreign_key)
# our foreign_key is missing
options[:foreign_key] = old_association.to_s.foreign_key
logger.info("Foreign_key was missing, is now: #{options[:foreign_key]}")
end
super
alias_method old_association, new_association
end
def self.has_many(association_id, options = {})
new_association = association_id.to_s.insert(0, 'legacy_').to_sym
old_association = association_id
logger.info "Legacy model has_many to association: '#{association_id}'"
association_id = association_id.to_s.insert(0, 'legacy_').to_sym
logger.info "Converting association to: '#{association_id}'"
logger.debug("Association options are: #{options.inspect}")
if options.has_key?(:through)
options[:through] = options[:through].to_s.insert(0, 'legacy_')
logger.info("Through mutated, is now: #{options[:through]}")
end
super
alias_method old_association, new_association
end
end
每当我尝试访问LegacyRole实例的权限时,我都会收到以下Active Record错误: ActiveRecord :: HasManyThroughAssociationNotFoundError:在模型LegacyRole中找不到关联“legacy_permissions_roles”
我尽可能地完成了这一切,我真的无法弄清楚为什么会发生这种情况,显然这比LegacyModel类的标准要复杂得多,我真的不知道如何诊断这进一步......我现在正处于这样的地步,在那里我看不到森林的树木,觉得它可能只是一些非常简单的东西我错过了!
修改
以下是模型加载的日志输出
****************************
Loaded legacy model: LegacyPermission using table: permissions
Legacy model has_many association: 'permissions_roles'
Converting association to: 'legacy_permissions_roles'
Association options are: {}
Legacy model has_many association: 'roles'
Converting association to: 'legacy_roles'
Association options are: {:through=>:permissions_roles}
Changed :through to: 'legacy_permissions_roles'
****************************
Loaded legacy model: LegacyPermissionsRole using table: permissions_roles
Legacy model has belongs_to association: 'role'
Converting association to: 'legacy_role'
Legacy model has belongs_to association: 'permission'
Converting association to: 'legacy_permission'
Foreign_key was missing, is now: 'permission_id'
****************************
Loaded legacy model: LegacyRole using table: roles
Legacy model has_many association: 'permissions_roles'
Converting association to: 'legacy_permissions_roles'
Association options are: {}
Legacy model has_many association: 'permissions'
Converting association to: 'legacy_permissions'
Association options are: {:through=>:permissions_roles}
Changed :through to: 'legacy_permissions_roles'
答案 0 :(得分:0)
也许你想要
class LegacyRole < LegacyModel
has_many :permissions_roles
has_many :permissions, :through => :legacy_permissions_roles # note the `legacy` prefix
end
或者这是你帖子中的拼写错误?