开发者

What is the Rails 3 Way to do a basic AR find on one object?

Is it:

u = User.find_by_email("me@myplace.com")

or:

u = User.where(:email => "me@my开发者_如何学编程place.com").first

I think I read that conditions are being phased out in 3.1.

I know you want to stick with AREL on retrieving multiple objects, because it figures out when to actually do the query (like if you're iterating through a collection). But I don't know what the convention is for grabbing singles.


I use both interchangeably.

I haven't read anything about the find_by methods being deprecated, but if your concern is that they will be, then use the second approach. You're only other option is to call last. You could also call all but that would return the object in an array.

Read section 1.1 Retrieving a Single Object of the Edge Guides article Active Record Querying for more information.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜