I持って、次の三つのモデルのActiveRecord :: HasManyThroughAssociationNotFoundError問題、ひねりを加えた
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
私は上のアクセス許可しようとするたびに のActiveRecord :: HasManyThroughAssociationNotFoundError:LegacyRoleのインスタンスが、私は次のActive Recordのエラーを取得し、私は私ができると私は本当にすることはできません最善として、このすべてを通じて段階まし
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'
。 私は、レールがクラスファイルのソースをチェックしていて、これに起因する矛盾を見つけていることを疑っています...確かに! – Lee
おっと、気づいていなかった...レールはクラスファイルをチェックしてはいけません。多くの場合、 'schema.rb'ファイルをチェックする必要があります。自動プレフィックスを付けずに試してみるといいかもしれません。また、おそらくモデルの反射を調べたいと思うかもしれません(私はこれについて簡単に書いています:http://davidsulc.com/blog/2011/06/19/leveraging-active-record-reflections/)正しく登録されました。 –
LegacyRoleのLegacyModel *および* have:through:>:legacy_permissions_rolesのオプション[:through]を変更しないと、実際にはあまり意味がない未知の列 'permissions_roles.legacy_role_id'が表示されますハック/フィックス)、それは私がLegacyModelが自動的にやっているはずのことだけを手でやっていることです...ありがとう、私はあなたのコメントが正しい軌道に乗っていると思います! – Lee