加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 在Rails 3活动记录查询中使用WHERE子句

发布时间:2020-12-17 03:46:18 所属栏目:百科 来源:网络整理
导读:我是rails的新手,我正在尝试使用where方法从我的数据表中检索记录.但是,使用它似乎不会返回任何结果. employee = Employee.where("first_name = ?","bill") #doesn't workemployee = Employee.where(:first_name = "bill") #doesn't workemployee = Employee
我是rails的新手,我正在尝试使用where方法从我的数据表中检索记录.但是,使用它似乎不会返回任何结果.

employee = Employee.where("first_name = ?","bill")  #doesn't work

employee = Employee.where(:first_name => "bill")  #doesn't work

employee = Employee.find_by_first_name("bill")  #works

我正在通过打印employee.first_name测试结果,就像我说前两个不返回任何内容而第三个返回“bill”.这里发生了什么?

解决方法

前两个将返回一个数组(所有具有该名称的员工). Employee.find_by_first_name将仅返回第一个结果 – Employee.find_all_by_first_name应返回与前两个查询类似的数组.

看看这是否符合您的期望:

Employee.where("first_name = ?","bill").first

(为了完整起见,Employee.where实际返回的是一个可链接的范围对象,其作用类似于数组)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读