【发布时间】:2010-09-23 19:23:36
【问题描述】:
我有重写 Ar 的 find 方法的库代码。我还包括所有 Association 类的模块,因此 MyModel.find 和 @parent.my_models.find 都可以工作并应用正确的范围。
我的代码基于 will_paginate 的:
a = ActiveRecord::Associations
returning([ a::AssociationCollection ]) { |classes|
# detect http://dev.rubyonrails.org/changeset/9230
unless a::HasManyThroughAssociation.superclass == a::HasManyAssociation
classes << a::HasManyThroughAssociation
end
}.each do |klass|
klass.send :include, Finder::ClassMethods
klass.class_eval { alias_method_chain :method_missing, :paginate }
end
我的问题是,我只想覆盖某些模型的查找器。目前我需要扩展所有模型共享的所有关联集合类。我知道我可以通过传递一个模块来扩展每个模型的关联:
has_many :things, :extend => SomeCustomMethods
但我的库基本上是 ActiveRecord 插件,所以我想要一个干净的可插入查找器扩展约定,适用于模型和作用域集合,而不影响应用程序中的所有模型。
【问题讨论】:
标签: ruby-on-rails ruby activerecord finder