Ruby-on-rails find, where 和 find_by_id 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15185919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between find, where and find_by_id?
提问by jvnill
What's the difference between find, whereand find_by_id? They all work when you try to find a user given an ID.
什么之间的区别find,where以及find_by_id?当您尝试查找给定 ID 的用户时,它们都会起作用。
回答by jvnill
The difference is what they return when a record is found, or when it's not found. Consider the following examples:
不同之处在于它们在找到记录或未找到记录时返回的内容。考虑以下示例:
>> User.create name: 'THE USER' # creates a user with id = 1
>> User.find(1) # returns the user
>> User.find_by_id(1) # returns the user
>> User.where(id: 1).first # returns the user
As you can see, an existing user can be fetched using any of the 3 methods. The big difference with using whereis you can chain commands (of course, without calling firstfirst.)
如您所见,可以使用 3 种方法中的任何一种来获取现有用户。using 的最大区别where在于您可以链接命令(当然,无需first先调用。)
Let's have a look at when you try to find a record that isn't existing
让我们来看看当您尝试查找不存在的记录时
>> User.find(2) # raises an exception
>> User.find_by_id(2) # nil
>> User.where(id: 2).first # nil
So here, it's obvious that when you use findto search for a record that isn't existing, you get an exception. That exception is ActiveRecord::RecordNotFoundwhich renders a 404 on production environment.
所以在这里,很明显,当您使用find搜索不存在的记录时,您会遇到异常。该例外是ActiveRecord::RecordNotFound在生产环境中呈现 404。
Hope this helps!
希望这可以帮助!
UPDATE
更新
Rails 4 uses the following syntax for find_by
Rails 4 使用以下语法 find_by
>> User.find_by(id: 1) # returns nil if there's no user with an id of 1
>> User.find_by!(id: 1) # raises ActiveRecord::RecordNotFound when no record is found
回答by Alanoud Just
find=> This return single record if the given primary_key(id) exists in the system otherwise in will give an error.
find=> 如果给定的 primary_key(id) 存在于系统中,则返回单个记录,否则将给出错误。
Model.find(required_id_value)
find_by=> This will return single record depends on the given attribute, and if the value of the attribute is not exist in the DB it will return nil.
find_by=> 这将返回取决于给定属性的单个记录,如果该属性的值在数据库中不存在,它将返回 nil。
Model.find_by_name("your name")
name here is the attribute and it must be exist in your Modal.
这里的名称是属性,它必须存在于您的模态中。
where=> This will return an active record relation with zero or more records you need to use first to return only one record or nil in case zero records return.
where=> 这将返回一个具有零个或多个记录的活动记录关系,您需要首先使用它来仅返回一个记录或在零记录返回的情况下返回 nil。
Model.where(id: id_value).first
回答by Rahul Tapali
find=> this is used to find row by id. This will return single record.
find=> 这用于查找 row by id。这将返回单个记录。
YourModel.find(2)
Address.find(1)
find_by=> this is used to get row by any attributes of record. This will return first matching record if condition matches.
find_by=> 这用于通过记录的任何属性获取行。如果条件匹配,这将返回第一个匹配记录。
YourModel.find_by_attrname("value")
Address.find_by_street_name_and_city("Andheri", "Newyork")
Addess.find_by_id(4)
where=> this is used get active records based on conditions to return active record relation (i.e.) may be zero or more records.
where=> 这用于根据条件获取活动记录以返回活动记录关系(即)可能是零个或多个记录。
YourModel.where(:attrname => "something")
Address.where(:city => "Newyork")
回答by Bilal A.Awan
Simple to me....
对我来说很简单....
'Find' returns single object to you as a result but 'Where' returns Array to you so you need to add .each to fetch each object from that array.
结果,'Find' 将单个对象返回给您,但 'Where' 将 Array 返回给您,因此您需要添加 .each 以从该数组中获取每个对象。

