闲来无事,结合以前的代码,总结了ruby on rails的查询方法,方便自己以后查看,也方便后来人,如下,欢迎批评指正

1::simpleDB
modules = find(:all, :conditions => ["site_id != '' and next_crawl < ? and next_crawl is not null and next_crawl != 'nil' and active='#{active}' and (in_queue is null or in_queue='nil' or in_queue='false')", Time.now],:order=>"next_crawl")
modules = Module.find(:all, :conditions => [" next_snapshot is not null and next_snapshot!='nil' and next_snapshot < ? and priority= ? and ready_for_queue = ? and uri not like 'http://weibo.com%'", Time.now, priority,true], :order=>"next_snapshot",:limit => limit)
modules = Module.select(:all, :conditions => ["date > ?", "#{(current-20.day).strftime('%Y-%m-%d')}"], :order => 'date desc')
modules = Module.find(post_id,:params => {:topic_id => topic_id}) 2::Mysql
Mysql使用find 进行查询
module = Module.find("id") Mysql默认 使用id 查询,直接find 即可
modules = Module.find(:all, :conditions=>['brand_id = ? and charged >?',@brand_id, 1.months.ago]).uniq 根据条件 查询所有,去重复
module = Module.find(:first,:conditions=>["member_id = ? and id = ?",session[:user_id],params[:job_id]])
module = Module.find(:all,:conditions=>["id in (?)",job_ids]) in job_ids 查询
module = Module.find(:all, :conditions=>['col_name = ?',col_val], :order=>"created_at") 按照某个字段order
Module.find(:all, :conditions => ['member_id = ?', @member.id]).map{|view| view.ft_id} 返回数据,值为ft_id的值
modules = Module.find(:all, :limit => limit_no, :where => ["id = '#{id}' and date <= '#{date}'"], :order => [:created_at, :desc]) 排序 等等
modules = Module.find(:all, :conditions=>['updated_at > ?', Time.now-20.minute],:order => 'id desc')
modules = Module.find(:all, :joins => [:join_col], :conditions =>['m_id = ?',m_id] )
modules = Module.find(:all,:conditions=>["date > ? and site = 'http://s.weibo.com' and keyword != ''",total_day.day.ago],:order=>["keyword asc","date desc"])
MemberSite.find(:all,:conditions=>["member_id = ?", @member.id]).group_by{|ms| ms.m_id} 内置方法之排序:group_by() 按照m_id 排序
Module.find(:first, :conditions=>['survey_id = ? and name = ?', survey_id, 'STORY'], :select=>'question_id') 什么意思
module = Module.find(:first, :order => "create_date, attending DESC") Mysql使用类似于jdbc的查询
results = ActiveRecord::Base.connection_pool.with_connection do |connection|
id = connection.quote(cuser_id)
start_at = Date.parse(start_time).to_time.to_i * 1000
end_at = Date.parse(end_time).to_time.end_of_day.to_i * 1000 sql = " select count(*) as num from user_follower where cuser_id = #{id} and user_verified_type != -1 and followed_at >= #{start_at} and followed_at <= #{end_at} " connection.execute(sql)
end result = ActiveRecord::Base.connection_pool.with_connection do |connection|
sql = "select count(user_id) as user_id_count from user_follower where cuser_id = '#{cuser_id}'"
connection.execute(sql)
end
@saved_followers_count = result.first[0] || 0 results = ActiveRecord::Base.connection_pool.with_connection do |connection|
id = connection.quote(cuser_id)
sql = " select year, month, day, post_type, count(*) as num , group_concat(concat(post_id,':',topic_id) SEPARATOR ',' ) as ids "+
" from posts_group_by_type_date_time where cuser_id = #{id} and " + generate_date_sql(startTime, endTime) + " group by year,month,day,post_type " connection.execute(sql)
end
把结果当做array返回 to_a
if(results.count == 0)
return []
else
return results.to_a
end mysql 使用where查询
Scope.where({:col => "XXX"}) 最简单的where查询
@industries = Scope.where({:module_type => "industry"}).map{|scope| scope.scope} 上面有介绍
@scope = Scope.where({:scope => params["scope"]}).first 返回第一个值
Threads.where("scope = ? and period = ?", m.id, period).count 根据条件查询,然后count
Analytics.where({:scope => m.id,:topic => 'all', :site_id=>'all', :date => start_time...end_time}).sum("posts") 另外一种赋值方式
Threads.where("scope = ? and topic like ? and period = ?", scope, "%#{topic}%", period).limit(50) limit查询

自己总结的ruby on rails 查询方法的更多相关文章

  1. rails的数据库查询方法

    rails的数据库查询方法 学习了:http://blog.csdn.net/menxu_work/article/details/8664962 学习了:http://www.cnblogs.com ...

  2. 【转】Ruby on Rails中select使用方法

    在Ruby on Rails中真的有一堆Select helper可以用,我们经常容易混淆.常见的有三个..select, select_tag, collection_select(其余的什么sel ...

  3. 开发新手最容易犯的50个 Ruby on Rails 错误(1)

    [编者按]本文最早发布与 JETRuby 博客,主要介绍了开发新手最容易犯的 Ruby 错误.文章系国内 ITOM 管理平台 OneAPM 编译呈现. 一年前,我们创立了以 "Rubyboo ...

  4. [技术博客] 软工-Ruby on Rails 后端开发总结分享

    [技术博客] 软工-Ruby on Rails 后端开发总结分享 在这次软件编写中,我们的后端使用了Ruby on Rails (RoR)框架. Rails框架是用Ruby编写的.这意味着当我们为Ru ...

  5. Linux超快速安装Ruby on Rails

    Linux超快速安装Ruby on Rails 时间 2014-11-25 11:45:11 Flincllck Talk 原文  http://www.flincllck.com/quick-ins ...

  6. rails查询mongodb通用查询

    ruby on rails 很好的跟mongodb进行了结合,gem包: 地址:https://rubygems.org/gems/mongoid 文档:https://docs.mongodb.co ...

  7. ruby on rails on windows

    这次想系统学会rails,最终目标是将redmine改造成顺手的工具,主要的手段就是开发redmine插件.虽然网上都推荐使用类Unix系统,可手头只有win7系统,就安装了. 难免会遇到这样那样的问 ...

  8. 通过Ruby On Rails 框架来更好的理解MVC框架

    通过Ruby On Rails 框架来更好的理解MVC框架   1.背景    因为我在学习软件工程课程的时候,对于 MVC 框架理解不太深入,只是在理论层面上掌握,但是不知道如何在开发中使用 MVC ...

  9. [ruby on rails] 跟我学之(9)删除数据

    首先需要在index页加个删除链接,并提供一个删除的确认,用户确认删除时,直接删除数据. 修改views 修改 app/views/posts/index.html.erb,如下: <h1> ...

随机推荐

  1. 解决 Xcode7 中多个模拟器的办法

    转自: http://www.oschina.net/code/snippet_196012_50574 1.关闭xcode 2.终端输入 sudo killall -9 com.apple.Core ...

  2. Android ListView两种长按弹出菜单方式

    转自:http://www.cnblogs.com/yejiurui/p/3247527.html package com.wyl.download_demo; import java.util.Ar ...

  3. .NET C#操作文件系统数据的常用方法总结 part1

    目录导航 1.0开启文件隐藏属性 1.1开启文件夹只读属性 1.2输出子文件夹路径 1.3统计文件数目 1.4批量重命名文件 1.5分割文件 1.6拼接文件 1.7读取用逗号分割的值 1.8读写压缩数 ...

  4. 转: Firefox 浏览器对 TABLE 中绝对定位元素包含块的判定有错误

    标准参考 元素的包含块 W3C CSS2.1 规范中规定,绝对定位元素的包含块(containing block),由离它最近的 position 特性值是 "absolute". ...

  5. 互联网创业十问?good(快速迭代、把握核心用户应对抄袭,不需要把商业模式考虑完备,4种失败的信号,失败者没资格说趁着年轻...)

    著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:曹政链接:https://www.zhihu.com/question/20264499/answer/28168079来源: ...

  6. Android学习笔记:ListView简单应用--显示文字列表

    在activity中的编写如下代码: final List<String> items = new ArrayList<String>(); //设置要显示的数据,这里因为是例 ...

  7. 亚马逊带Marketplace product code的image无法再mount到其他镜像上

    这是对已发布镜像的保护么?难道对其进行修改的路彻底断掉了? 以volume形式attach也不行,dd成raw再读取也读不了,敢问路在何方呢 If a volume has an AWS Market ...

  8. Nmon 性能:分析 AIX 和 Linux 性能的免费工具

    原文摘自: http://www.ibm.com/developerworks/cn/aix/library/analyze_aix/ 官网:http://www.ibm.com/developerw ...

  9. java--创建多线程两种方法的比较

    [通过继承Thread] 一个Thread对象只能创建一个线程,即使它调用多次的.start()也会只运行一个的线程. [看下面的代码 & 输出结果] package Test; class ...

  10. 基于visual Studio2013解决算法导论之011快排改良

     题目 快排改良 解决代码及点评 #include <stdio.h> #include <stdlib.h> #include <malloc.h> #in ...