ruby on rail数据库查询

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

[sql]  view plain copy
  1. 1::simpleDB  
  2.     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")  
  3.     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)  
  4.     modules = Module.select(:all, :conditions => ["date > ?""#{(current-20.day).strftime('%Y-%m-%d')}"], :order => 'date desc')  
  5.     modules = Module.find(post_id,:params => {:topic_id => topic_id})  
  6.       
  7. 2::Mysql   
  8.    Mysql使用find 进行查询  
  9.     module = Module.find("id")  Mysql默认 使用id 查询,直接find 即可  
  10.     modules = Module.find(:all, :conditions=>['brand_id = ? and charged >?',@brand_id, 1.months.ago]).uniq 根据条件 查询所有,去重复  
  11.     module = Module.find(:first,:conditions=>["member_id = ? and id = ?",session[:user_id],params[:job_id]])  
  12.     module = Module.find(:all,:conditions=>["id in (?)",job_ids])  in  job_ids 查询  
  13.     module = Module.find(:all, :conditions=>['col_name = ?',col_val], :order=>"created_at") 按照某个字段order  
  14.     Module.find(:all, :conditions => ['member_id = ?', @member.id]).map{|viewview.ft_id} 返回数据,值为ft_id的值  
  15.     modules = Module.find(:all, :limit => limit_no, :where => ["id = '#{id}' and date <= '#{date}'"], :order => [:created_at, :desc]) 排序 等等  
  16.     modules = Module.find(:all, :conditions=>['updated_at > ?'Time.now-20.minute],:order => 'id desc')  
  17.     modules = Module.find(:all,  :joins => [:join_col], :conditions =>['m_id = ?',m_id] )  
  18.     modules = Module.find(:all,:conditions=>["date > ? and site = 'http://s.weibo.com' and keyword != ''",total_day.day.ago],:order=>["keyword asc","date desc"])  
  19.     MemberSite.find(:all,:conditions=>["member_id = ?", @member.id]).group_by{|ms| ms.m_id}  内置方法之排序:group_by() 按照m_id 排序  
  20.     Module.find(:first, :conditions=>['survey_id = ? and name = ?', survey_id, 'STORY'], :select=>'question_id') 什么意思  
  21.     module = Module.find(:first, :order => "create_date, attending DESC")  
  22.   
  23.    Mysql使用类似于jdbc的查询  
  24.     results = ActiveRecord::Base.connection_pool.with_connection do |connection|  
  25.         id = connection.quote(cuser_id)  
  26.             start_at = Date.parse(start_time).to_time.to_i * 1000  
  27.             end_at = Date.parse(end_time).to_time.end_of_day.to_i * 1000  
  28.   
  29.             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} "  
  30.   
  31.             connection.execute(sql)  
  32.         end  
  33.   
  34.     result = ActiveRecord::Base.connection_pool.with_connection do |connection|  
  35.               sql = "select count(user_id) as user_id_count from user_follower where cuser_id = '#{cuser_id}'"  
  36.               connection.execute(sql)  
  37.          end  
  38.          @saved_followers_count = result.first[0] || 0  
  39.   
  40.     results = ActiveRecord::Base.connection_pool.with_connection do |connection|  
  41.             id = connection.quote(cuser_id)  
  42.             sql = " select year, month, day, post_type, count(*) as num , group_concat(concat(post_id,':',topic_id) SEPARATOR ',' ) as ids "+  
  43.                 " from posts_group_by_type_date_time where cuser_id = #{id} and " + generate_date_sql(startTime, endTime) + " group by year,month,day,post_type "  
  44.   
  45.             connection.execute(sql)  
  46.         end  
  47.         把结果当做array返回 to_a  
  48.         if(results.count == 0)  
  49.             return []  
  50.         else  
  51.             return results.to_a  
  52.         end  
  53.   
  54.   
  55.     mysql 使用where查询  
  56.     Scope.where({:col => "XXX"}) 最简单的where查询  
  57.     @industries = Scope.where({:module_type => "industry"}).map{|scope| scope.scope} 上面有介绍  
  58.     @scope = Scope.where({:scope => params["scope"]}).first  返回第一个值  
  59.     Threads.where("scope = ? and period = ?", m.id, period).count 根据条件查询,然后count  
  60.     Analytics.where({:scope => m.id,:topic => 'all', :site_id=>'all',  :date => start_time...end_time}).sum("posts") 另外一种赋值方式  

  1.     Threads.where("scope = ? and topic like ? and period = ?", scope, "%#{topic}%", period).limit(50)  limit查询  





转载至:http://blog.csdn.net/cloudcraft/article/details/9343089

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值