• 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没有查询到结果,会返回nil

  • where
    where返回的是一个ActiveRecord_Relation集合,并不是一个model的对象,像product = Product.where("id = 1"),查询后,这样使用product.id是不行的,需要这样prodcut.first.id或者product.take.id

最新文章

  1. SQL Server 批量删除存储过程
  2. python之路十五
  3. Newtonsoft.Json
  4. jQuery Ajax MVC 下拉框联动
  5. 最小安装模式下Centos7.*网卡启动配置
  6. fork与vfork
  7. [问题] UISearchBar 点击取消后跳动的问题
  8. IntelliJ IDEA License
  9. 应付分配集 Distribution Sets
  10. mysqld --debug-sync
  11. 如此的相似,不能-------Day84
  12. python基本运算
  13. C语言顺序栈
  14. SQL Server中调用WebService
  15. eclipse中的ctrl+H使用中的问题
  16. 系统服务和普通FORMS程序共存一体的实现
  17. SRM479
  18. WinFrom下Webbrowser加载自定义页面的技巧
  19. linux环境中查看主机型号(机器型号)
  20. 快速切题CF 158B taxi 构造 && 82A double cola 数学观察 难度:0

热门文章

  1. haskell 乱搞笔记[原创]
  2. bzoj1734 [Usaco2005 feb]Aggressive cows 愤怒的牛 二分答案
  3. bzoj2648/2716 kdtree
  4. kubernetes---CentOS7安装kubernetes1.11.2图文完整版
  5. android soap webservers 无法执行 报错 ht.call(SOAP_ACTION, envelope);解决方法
  6. Java面试题集(二)
  7. AC日记——凌乱的yyy 洛谷 P1803
  8. 存code
  9. Go -- 判断chan channel是否关闭的方法
  10. Oracle 12c agent install for linux