Where should I start looking to create a plugin that extends Rails 3 finds?
Prior to Rails 3, creating a plugin to extend find
was relatively easy: basically override the find
method itself in ActiveRecord::Base
, 开发者_开发知识库calling super
if needed.
Now that Arel is being used in Rails 3, (specifically I'm using Rails 3.1), how would I go about doing something similar? The problem is that many of the old find
methods are deprecated in favor of scopes like where
, order
, limit
, etc. At what point(s) in the Rails source should I try to override the default behavior?
I'm sure it's going to be a bit more convoluted than this, but the closest thing I can find that seems like it might be appropriate is the construct_finder_arel
method in ActiveRecord::Base
.
After digging through Rails source, regardless of Arel being used, find_by_sql
method is called on whatever model is performing the find. This can be extended via alias_method_chain
as follows:
find_by_sql_with_customization(*args)
results = find_by_sql_without_customization
# do something with results here
results
end
alias_method_chain :find_by_sql, :customization
精彩评论