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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.