使用ActiveRecord 3 / Arel查找单个记录的最佳方法? [英] Best way to find a single record using ActiveRecord 3 / Arel?

查看:64
本文介绍了使用ActiveRecord 3 / Arel查找单个记录的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前经常这样做的地方:

Where I used to do this:

Foo.find_by_bar('a-value')

我现在可以执行以下操作:

I can now do this:

Foo.where(:bar => 'a-value').limit(1).first

这是推荐的吗?这是最好的方法吗?我应该继续使用旧的方法,因为它仍然是有用的语法糖,还是现在有一种更好的方法可以支持链接和所有其他好东西呢?

Is this recommended? Is this the best way? Should I continue to use the "old" way because it continues to be useful syntactic sugar, or is there an Even Better way I can do that now, which will support chaining and all the other good stuff?

推荐答案

第4条:

Foo.find_by bar: 'a_value' , wibble: 'a wibble value'

这篇关于使用ActiveRecord 3 / Arel查找单个记录的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆