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
rails find find_by where的更多相关文章
- 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 ...
- Rails :布局和视图渲染
原文地址: http://guides.ruby-china.org/layouts_and_rendering.html Rails 布局和视图渲染 本文介绍 Action Controller 和 ...
- Ruby on Rails Tutorial 第六章 用户模型
1.用户模型(1)数据库迁移Rails默认使用关系数据库存储数据,数据库中的表有数据行组成,每一行都有相应的列,对应数据属性.把列名命名为相应的名字后,ActiveRecord会自动把他们识别为用户对 ...
- rails 杂记 - render and layout
官方文档:http://guides.rubyonrails.org/layouts_and_rendering.html 渲染 view 渲染 html.rb 与相应的 action control ...
- (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 ...
- 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_ ...
- 4-2 什么是WebSocket; Action Cable的使用。Rails guide-6.3视频教学,没有看!
WebSocket WebSocket是一种在单个TCP连接上进行全双工通讯的协议.WebSocket通信协议于2011年被IETF定为标准RFC 6455,并由RFC7936补充规范.WebSock ...
- Rails 5 Test Prescriptions 第3章Test-Driven Rails
本章,你将扩大你的模型测试,测试整个Rails栈的逻辑(从请求到回复,使用端到端测试). 使用Capybara来帮助写end-to-end 测试. 好的测试风格,包括端到端测试,大量目标明确的单元测试 ...
- rails 常用方法
bundle install --without production 不安装production中的gem ./configure && make && sudo m ...
随机推荐
- 算法复习——网络流模板(ssoj)
题目: 题目描述 有 n(0<n<=1000)个点,m(0<m<=1000)条边,每条边有个流量 h(0<=h<35000),求从点 start 到点 end 的最 ...
- cf682E Alyona and Triangles
You are given n points with integer coordinates on the plane. Points are given in a way such that th ...
- asp.net mvc 页面内容呈现Html.Raw HtmlString
asp.net mvc 页面内容呈现Html.Raw Html.Raw内容经过页面呈现,不呈现Html标签 @Html.Raw( File.ReadAllText(Server.MapPath(&qu ...
- 区间翻转(codevs 3243)
题目描述 Description 给出N个数,要求做M次区间翻转(如1 2 3 4变成4 3 2 1),求出最后的序列 输入描述 Input Description 第一行一个数N,下一行N个数表示原 ...
- Google解决跨域
1.添加 --disable-web-security --user-data-dir=D:\tmp 2.在D的根目录新建tmp文件夹
- TCP/IP 协议栈
TCP(传输控制协议) 传输控制协议(Transmission Control Protocol,TCP)是一种面向连接的.可靠的.基于字节流的传输层通信协议,由IETF的RFC 793定义. 在因特 ...
- Linux下&/jobs/fg/bg命令的使用(转)
一.& 这个用在一个命令的最后,可以把这个命令放到后台执行. 二.[Ctrl]+[Z] 可以将一个正在前台执行的命令放到后台,并且暂停. 三.jobs 查看当前有多少在后台运行的命令. 四.f ...
- 超级强大的淘宝开源平台(taobao-code)
今天发现了一个免费又高级的开源SVN服务器,taobao,阿里云CODE.迫不及待的注册了一个.感觉不错,分享给大家. 先说说我们用过的几个SVN服务器吧: google code oksvn(感觉不 ...
- 【嵌入式Linux+ARM】GPIO操作
1.GPIO介绍 GPIO(general purpose i/o ports)意思为通用输入/输出端口,通俗的说就是一些引脚. 我们可以通过它们输出高低电平 或 读入引脚的状态. s3c2440中有 ...
- 【paddle学习】词向量
http://spaces.ac.cn/archives/4122/ 关于词向量讲的很好 上边的形式表明,这是一个以2x6的one hot矩阵的为输入.中间层节点数为3的全连接神经网络层,但你看右 ...