我应该从哪里开始寻找创建一个扩展 Rails 3 发现的插件?

发布于 2024-11-27 19:26:43 字数 461 浏览 0 评论 0原文

在 Rails 3 之前,创建一个插件来扩展 find 相对容易:基本上覆盖 ActiveRecord::Base 中的 find 方法本身,调用 <如果需要,代码>超级。

既然 Arel 正在 Rails 3 中使用(具体来说我正在使用 Rails 3.1),我将如何做类似的事情?问题是许多旧的 find 方法已被弃用,取而代之的是 whereorderlimit 等范围等等。我应该在 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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒几许 2024-12-04 19:26:43

深入研究 Rails 源代码后,无论是否使用 Arel,都会在执行查找的任何模型上调用 find_by_sql 方法。这可以通过 alias_method_chain 扩展,如下所示:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文