`

ruby condition

    博客分类:
  • ruby
 
阅读更多

class.new 新建
class.find 查询
class.destroy 删除

变量查询
a="hahaha"
Product.find(:all,:conditions=>["title like ?","%#{a}%"])
Product.find(:all,:conditions=>["title like :title",:title=>"%#{a}%"])
Product.find(:all,:conditions=>["title like :title and price>:price",:title=>"%#{a}%",:prcie=>3])
相当
SELECT * FROM "products" WHERE (title like '%a%' and price>3)

字符串查询
Product.find(:all,:conditions=>{:title=>"a"})
相当
SELECT * FROM "products" WHERE ("products"."title" = 'a')

多条件查询合并
>> cs = [{:title=>"a",:price=>(1..20),:description=>"hhhhh"}, "title like '%b%'"]
=> [{:price=>1..20, :title=>"a", :description=>"hhhhh"}, "title like '%b%'"]
>> Product.all :conditions=> Product.merge_conditions(*cs)
相当
SELECT * FROM "products" WHERE (("products"."title" = 'a' AND "products"."price" BETWEEN 1 AND 20 AND "products"."description" = 'hhhhh') AND (title like '%b%'))

把条件设置数组
>> conditions = [] #定义一个数组
=> []
>> conditions << ["title like ?", 'a'] #把一个条件加到数组
=> [["title like ?", "a"]]
>> conditions << ["title like ?", 'a'] if params[:title].present? #加一个判断 非空时加入到数组

include附加查询(减少N+1次查询)
LineItem.all :conditions => "products.title => 'a'", :include => :product

jions附加查询
LineItem.all :conditions => "products.title like '%a%'", :joins => :product
想当
SELECT "line_items".* FROM "line_items" INNER JOIN "products" ON "products".id = "line_items".product_id WHERE (products.title like '%a%')

select查询
Product.find(:all,:select=>"title,price")
相当
SELECT title,price FROM "products"

readonly只读查询
>> p=Product.first
=> #.....
>> p=Product.first(:readonly=>true)
=> #....
>> p.title="xxxxxxxxx"
=> "xxxxxxxxx"
>> p.save #抛出异常

from 指定表名
group 指定分组
limit 指定条数
offset 指定起始数
find_by_sql 直接执行sql语句

获取字段统计信息
Product.average(:price)
Product.maximum(:price)
Product.minimum(:price)
Product.sum(:price)
Product.count()

动态查询
Product.find_by_title_and_price("测试",78.9) #只查第一条first 结果:title和price
Product.find_all_by_title_and_price("测试",78.9) #返回数组 结果:title和price
Product.find_or_create_by_title("hahahaha") #查询并保存
Product.find_or_initialize_by_title("aoiokkok") #查询,如果没有初始化


查看日志
tail -f log/development.log

分享到:
评论

相关推荐

    furima-34568

    Ruby版本 系统依赖 配置 数据库创建 数据库初始化 如何运行测试套件 服务(作业队列,缓存服务器,搜索引擎等) 部署说明 ... 数据库设计 用户表 柱子 类型 选项 电子邮件 细绳 null:假,唯一:true 加密密码 ...

    furima-29723

    您可能要讲的内容: Ruby版本系统依赖配置数据库创建数据库初始化如何运行测试套件服务(作业队列,缓存服务器,搜索引擎等) 部署说明...テーブル设计用户表柱子类型选项昵称细绳空假加密密码细绳空假电子邮件细绳...

    ransack_query:提供一种构建高级搜索查询的方法

    安装将此行添加到应用程序的 Gemfile 中: gem 'ransack_query'然后执行: $ bundle或者自己安装: $ gem install ransack_query用法简单用法: RansackQuery.build do |grouping| grouping.add_condition...

    json_api_client_mock:轻松模拟JsonApiClient查询

    JsonApiClientMock 该gem可让您轻松测试 gem。 用法 在您的Gemfile : group :test do gem 'json_api_client_mock', '~...MyResource.where(condition1: 'value', condition2: 'value2').all =&gt; [&lt;#MyResource f

    红宝石激光在反射式弹道高速摄影中的应用

    This is a brief description of a pulsed ruby laser photographic system. It has the characteristics which make it an excellent source in a front lighted technique for obtaining photographs of models in...

    the-wall:Alpha Camp在线训练营演示日项目,The Wall-独立音乐平台

    COMPLETED /修正artists show.html.erb、partial template event_list.html.erb、events posts.html.erb和show.html.erb中if的condition写法。 COMPLETED /新增database indexesSam's suggestion: 1. C

    Maven权威指南 很精典的学习教程,比ANT更好用

    Maven权威指南 Authors Tim O'Brien (Sonatype, Inc.) , John Casey (Sonatype, Inc.) , Brian Fox (Sonatype, Inc.) , Bruce Snyder () , Jason Van Zyl (Sonatype, Inc.) , Juven Xu () Abstract ...

    furima-33531

    自述文件 数据库设计 用户表 柱子 类型 选项 昵称 细绳 null:假 ... item_condition_id 整数 null:false.foreign_key:true category_id 整数 null:false.foreign_key:true shipping_fee_id

    furima-33534

    ##テーブル设计 用户テーブル ...condition_id 整数 null:假 cost_id 整数 null:假 prefecture_id 整数 null:假 shipping_day_id 整数 null:假 用户 参考资料 null:false,foreign_key:true

    furima-34536

    テーブル设计 用户テーブル 柱子 类型 选项 ...condition_id 整数 null:假 consignor_area_id 整数 null:假 delivery_fee_id 整数 null:假 prepare_date_id 整数 null:假 用户 参考 null:fals

    CognitoTest:在浏览器中使用亚马逊 Cognito 服务的演示,使用一个 Rails 项目

    认知演示 使用从采样的代码 ... 为了让 Cognito 正常工作,非常精确地定义您帐户的未经身份验证的用户角色非常重要,确保它与可以承担此角色的实体之间的信任关系得到很好的定义。... "Condition": {

    furima-23506

    テーブル设计 ...condition_id 整数 null:假 shipping_charge_id 整数 null:假 prefectures_id 整数 null:假 pays_to_ship_id 整数 null:假 价格 整数 nill:错误 用户 参考 null:false,fo

    furima-35236

    many:项目has_many:订单项目テーブル柱子类型选项项目名称细绳null:假item_info 文本null:假category_id 整数null:假condition_id 整数null:假shipping_fee_id 整数null:假shipping_area_id 整数null:假...

    furima-35207

    テーブル设计 ...condition_id 整数 null:假 postage_id 整数 null:假 prefecture_id 整数 null:假 day_id 整数 null:假 价格 整数 null:假 用户 参考 null:false,foreign_key:true 协会

    furima-34016

    テーブル设计 用户テーブル 柱子 ...condition_id 整数 null:假 shipping_charge_id 整数 null:假 shipping_area_id 整数 null:假 day_to_ship_id 整数 null:假 价格 整数 null:假 用户 参考

    furima-32380

    自述文件 テーブル设计 用户テーブル ...condition_id 整数 null:假 charge_id 整数 null:假 发货编号 整数 null:假 day_id 整数 null:假 价格 整数 null:假 用户 参考 null:false,foreign_key:tru

    furima-33975

    condition_id 整数 null:假 shipping_charge_id 整数 null:假 area_id 整数 null:假 shipping_date_id 整数 null:假 用户 参考资料 null:false,foreign_key:true 协会 当属:用户has_one:购

    furima-32563

    テーブル设计 用户テーブル 柱子 类型 选项 ...condition_id 整数 null:假 fee_id 整数 null:假 prefecture_id 整数 null:假 day_id 整数 null:假 价格 整数 null:假 用户 参考 外键:真 协会

    furima-34787

    自述文件 テーブル设计 用户テーブル 柱子 类型 选项 ...condition_id 整数 null:假 shipping_cost_id 整数 null:假 prefecture_id 整数 null:假 days_till_ship_id 整数 null:假 价格 整数

    furima-34448

    假协会has_many:项目has_many:购买项目テーブル柱子类型选项项目名称细绳null:假item_text 文本null:假category_id 整数null:假condition_id 整数null:假delivery_fee_id 整数null:假shipping_source_id 整数...

Global site tag (gtag.js) - Google Analytics