u = User.find(1)
returns one User
#return a Set of users. In your case its only one user.
u = User.where("username = ?", "percent20")
The result type is ActiveRecord::Relation --> [User, User, User]
use e.g. first to get the first User
#returns the first user
u = User.where("username = ?", "percent20").first
u.class.name => "User"
====================================================
find 会直接返回查找到的记录,如果没有记录,则为nil
where 返回的是 ActiveRecord::Relation,并不是直接的结果,需要使用first,last,all等方法才能取到值
大多数情况下使用where
参考:http://m.onkey.org/active-record-query-interface
转载自:
http://stackoverflow.com/questions/5213358/difference-between-find-and-where-with-relationships
http://stackoverflow.com/questions/11161663/find-vs-find-by-vs-where