我正在使用 Ruby on Rails v3.2.2。alias_method
通过这种方式,我与用于关联的关联具有以下关联has_many :through
:
class Resource < ActiveRecord::Base
has_many :article_associations
alias_method :post_associations, :article_associations
has_many :users, :through => :post_associations
end
当我执行时,@article.users
我收到以下错误:
ActiveRecord::HasManyThroughAssociationNotFoundError
Could not find the association :post_associations in model Article
为什么会发生?有没有办法让它按预期工作?也就是说,如何@article.users
通过“aliased”正确返回:post_associations
?
注意:我想:post_associations
在has_many :users, :through => :post_associations
语句中“声明”/“使用”,因为我正在尝试“动态构建”(即元编程)has_many :through
关联。
Ruby on Rails框架跟踪是:
activerecord (3.2.2) lib/active_record/reflection.rb:501:in `check_validity!'
activerecord (3.2.2) lib/active_record/associations/association.rb:26:in `initialize'
activerecord (3.2.2) lib/active_record/associations/collection_association.rb:24:in `initialize'
activerecord (3.2.2) lib/active_record/associations/has_many_through_association.rb:10:in `initialize'
activerecord (3.2.2) lib/active_record/associations.rb:157:in `new'
activerecord (3.2.2) lib/active_record/associations.rb:157:in `association'
activerecord (3.2.2) lib/active_record/associations/builder/association.rb:44:in `block in define_readers'
...