如何覆盖“where”在轨道 3 中
我已将我的应用程序从 Rails 2.3.8 升级到 3.0.3 。但我面临一个问题。我使用的是“find”,但覆盖在 Rails 3 中不起作用:
# override activerecord's find to allow us to find by name or id transparently
def self.find(*args)
if args.is_a?(Array) and args.first.is_a?(String) and (args.first.index(/[a-zA-Z\-_]+/) or args.first.to_i.eql?(0) )
find_by_login_slug(args)
else
super
end
end
我想知道是否有一种方法可以在 Rails 3 中实现此功能,甚至可以使用 where 来代替。
谢谢
I have upgraded my application from rails 2.3.8 to 3.0.3 . But I'm facing a problem. I was using 'find' but the overriding doesn't work in rails 3:
# override activerecord's find to allow us to find by name or id transparently
def self.find(*args)
if args.is_a?(Array) and args.first.is_a?(String) and (args.first.index(/[a-zA-Z\-_]+/) or args.first.to_i.eql?(0) )
find_by_login_slug(args)
else
super
end
end
I'm wondering if there is a way to make this work in rails 3 or even by using where instead.
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您面临的问题是从 Rails 2.3.X 升级到 Rails 3.0.X 应用程序。尽管这看起来似乎是一项简单的任务,但事实并非如此,特别是如果您有一个真正的应用程序而不是一个玩具应用程序。我建议您观看 Rayn Bates 的截屏视频系列,您可以从 http://railscasts.com/episodes/225-upgrading-to-rails-3-part-1 以全面了解您面临的问题。
如果您只需要阅读有关ActiveRecord新界面的信息 http://m.onkey.org/active -record-query-interface 是一篇很棒的文章。
The problem you're are facing is an upgrading from a rails 2.3.X to a rails 3.0.X application. Although, it could seem a simple task it isn't, especially if you have a real application and not a toy one. I suggest you to take a look to a screencast series from Rayn Bates, you could start from http://railscasts.com/episodes/225-upgrading-to-rails-3-part-1 to get a complete idea off the problem you are facing.
If you only need to read about ActiveRecord new interface http://m.onkey.org/active-record-query-interface is a great article.