我应该从哪里开始寻找创建一个扩展 Rails 3 发现的插件?
在 Rails 3 之前,创建一个插件来扩展 find
相对容易:基本上覆盖 ActiveRecord::Base
中的 find
方法本身,调用 <如果需要,代码>超级。
既然 Arel 正在 Rails 3 中使用(具体来说我正在使用 Rails 3.1),我将如何做类似的事情?问题是许多旧的 find
方法已被弃用,取而代之的是 where
、order
、limit
等范围等等。我应该在 Rails 源代码的哪个点尝试覆盖默认行为?
我确信它会比这更复杂一些,但我能找到的最接近的似乎可能合适的是 ActiveRecord::Base
中的 construct_finder_arel
方法代码>.
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
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
深入研究 Rails 源代码后,无论是否使用 Arel,都会在执行查找的任何模型上调用 find_by_sql 方法。这可以通过
alias_method_chain
扩展,如下所示: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 viaalias_method_chain
as follows: