• 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

rails find find_by where的更多相关文章

  1. 12月15日 session:Ruby on Rails Security Guide//从第3节开始没有学习//关于find_by 和where的区别用法思考。

    http://guides.rubyonrails.org/security.html#user-management 2.session笔记见13日的随笔. http://www.cnblogs.c ...

  2. Rails :布局和视图渲染

    原文地址: http://guides.ruby-china.org/layouts_and_rendering.html Rails 布局和视图渲染 本文介绍 Action Controller 和 ...

  3. Ruby on Rails Tutorial 第六章 用户模型

    1.用户模型(1)数据库迁移Rails默认使用关系数据库存储数据,数据库中的表有数据行组成,每一行都有相应的列,对应数据属性.把列名命名为相应的名字后,ActiveRecord会自动把他们识别为用户对 ...

  4. rails 杂记 - render and layout

    官方文档:http://guides.rubyonrails.org/layouts_and_rendering.html 渲染 view 渲染 html.rb 与相应的 action control ...

  5. (Go rails)使用Rescue_from(ActiveSupport:Rescuable::ClassMethods)来解决404(ActiveRecord::RecordNotFound)❌

    https://gorails.com/episodes/handle-404-using-rescue_from?autoplay=1 我的git: https://github.com/chent ...

  6. 12月13日 什么是help_method,session的简单理解, find_by等finder method

    helper_method Declare a controller method as a helper. For example, helper_method :link_to def link_ ...

  7. 4-2 什么是WebSocket; Action Cable的使用。Rails guide-6.3视频教学,没有看!

    WebSocket WebSocket是一种在单个TCP连接上进行全双工通讯的协议.WebSocket通信协议于2011年被IETF定为标准RFC 6455,并由RFC7936补充规范.WebSock ...

  8. Rails 5 Test Prescriptions 第3章Test-Driven Rails

    本章,你将扩大你的模型测试,测试整个Rails栈的逻辑(从请求到回复,使用端到端测试). 使用Capybara来帮助写end-to-end 测试. 好的测试风格,包括端到端测试,大量目标明确的单元测试 ...

  9. rails 常用方法

    bundle install --without production 不安装production中的gem ./configure && make && sudo m ...

随机推荐

  1. BZOJ 3110 [Zjoi2013]K大数查询 ——树套树

    [题目分析] 外层区间线段树,内层是动态开点的权值线段树. SY神犇说树套树注重的是内外层的数据结构的选择问题,果然很重要啊. 动态开点的实现方法很好. [代码] #include <cstdi ...

  2. mybatis学习(一)——mybatis简介

    1.简介 MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBati ...

  3. spring security 登录、权限管理配置

    登录流程 1)容器启动(MySecurityMetadataSource:loadResourceDefine加载系统资源与权限列表)  2)用户发出请求  3)过滤器拦截(MySecurityFil ...

  4. 装B技能GET起来!Apple Pay你会用了吗?

    科技圈儿有一个自带光环的品牌 它每次一有任何动静 不用宣传 也不用刻意营销 消息还是能传天下 2月18日 你敢说你的朋友圈儿没有被下面这个词儿刷屏? Apple Pay 这不,我就跟着凑凑热闹,开个小 ...

  5. Chrome 浏览器安装Vue插件方法 (十分详细)

    博主最近在研究Vue,无奈新手想安装Chrome的Vue插件,整理下安装流程: 1.首先去github下载vue.zip文件插件(还有npm安装方法这里就不介绍了自行百度)下载地址:https://g ...

  6. Linux下使用curl进行http请求(转)

    curl在Linux下默认已经安装,Windows需要自行安装. 下载地址:https://curl.haxx.se/download.html Windows离线版本:链接:http://pan.b ...

  7. 【hibernate】报错:org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.DataException: could not execute statement

    报错如下: org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a ...

  8. 【Android开发—智能家居系列】(三):手机连接WIFI模块

    版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 概述 实现连接WIFI的功能会用到一个工具类,源码可以点击链接下载.网上这些类似的工具类里的代码差不多是一样的.连接无线网主要有 ...

  9. pandas入门指南

    上一篇讲了numpy,除此之外,还有一个工具我们一定会使用,那就是pandas.如果说numpy中数据存储形式是列表的话,那么pandas中数据的存储形式更像是字典.为什么这么说呢?因为pandas中 ...

  10. hadoop2.7.1 nutch2.3 二次开发windows环境

     Hadoop windows编译: 能够略过这一段,直接下载hadoo2.7.1 bin文件.我的资源里有终于生成的winutils.exe和一些native code,放在bin文件夹即可了 參 ...