rails find find_by where
find
根据id进行查询,像Product.find(3)
,查询语句是Product Load (0.1ms) SELECT "products".* FROM "products" WHERE "products"."id" = ? LIMIT ? [["id", 3], ["LIMIT", 1]]
,也可以直接传一个Product的对象,像product = Product.first Product.find(product)
find会把传过去的model对象的id进行查询。甚至可以这样:
user = User.last Product.find(user)
find会把user的id的值进行查询。
- 也可以Product.find([user1, user2, user3])
find没有查询到结果,会抛出一个
ActiveRecord::RecordNotFound
异常。find_by
需要传递一个hash作为参数。像Product.find_by(id:3)
,查询语句是Product Load (0.1ms) SELECT "products".* FROM "products" WHERE "products"."id" = ? LIMIT ? [["id", 3], ["LIMIT", 1]]
。当然也可以查询其它的字段,像Product.find_by(title: 'the yellow book'
,查询语句是Product Load (0.1ms) SELECT "products".* FROM "products" WHERE "products"."title" = ? LIMIT ? [["title", "the yellow book"], ["LIMIT", 1]]
find_by没有查询到结果,会返回nilwhere
where返回的是一个ActiveRecord_Relation
集合,并不是一个model的对象,像product = Product.where("id = 1")
,查询后,这样使用product.id
是不行的,需要这样prodcut.first.id
或者product.take.id
最新文章
- SQL Server 批量删除存储过程
- python之路十五
- Newtonsoft.Json
- jQuery Ajax MVC 下拉框联动
- 最小安装模式下Centos7.*网卡启动配置
- fork与vfork
- [问题] UISearchBar 点击取消后跳动的问题
- IntelliJ IDEA License
- 应付分配集 Distribution Sets
- mysqld --debug-sync
- 如此的相似,不能-------Day84
- python基本运算
- C语言顺序栈
- SQL Server中调用WebService
- eclipse中的ctrl+H使用中的问题
- 系统服务和普通FORMS程序共存一体的实现
- SRM479
- WinFrom下Webbrowser加载自定义页面的技巧
- linux环境中查看主机型号(机器型号)
- 快速切题CF 158B taxi 构造 &;&; 82A double cola 数学观察 难度:0
热门文章
- haskell 乱搞笔记[原创]
- bzoj1734 [Usaco2005 feb]Aggressive cows 愤怒的牛 二分答案
- bzoj2648/2716 kdtree
- kubernetes---CentOS7安装kubernetes1.11.2图文完整版
- android soap webservers 无法执行 报错 ht.call(SOAP_ACTION, envelope);解决方法
- Java面试题集(二)
- AC日记——凌乱的yyy 洛谷 P1803
- 存code
- Go -- 判断chan channel是否关闭的方法
- Oracle 12c agent install for linux