草庐IT

find-by-sql

全部标签

sql - 如何使用 ActiveRecord 模型在日期范围内进行搜索?

我是Ruby和ActiveRecord的新手。我目前需要修改现有的一段代码以在选择中添加日期范围。当前的作品是这样的:ReportsThirdparty.find(:all,:conditions=>{:site_id=>site_id,:campaign_id=>campaign_id,:size_id=>size_id})现在,我需要添加一个范围,但我不确定如何做BETWEEN或>=或运营商。我想我需要的是类似于:ReportsThirdparty.find(:all,:conditions=>{:site_id=>site_id,:campaign_id=>campaign_id

sql - 使用 SQL IN 和 SQL OR 运算符的 Rails 3 ActiveRecord 查询

我正在使用“where”语法编写一个Rails3ActiveRecord查询,它同时使用了SQLIN和SQLOR运算符,但不知道如何同时使用它们。此代码有效(在我的用户模型中):Question.where(:user_id=>self.friends.ids)#note:self.friends.idsreturnsanarrayofintegers但是这段代码Question.where(:user_id=>self.friends.idsOR:target=>self.friends.usernames)返回这个错误syntaxerror,unexpectedtCONSTANT,

sql - ActiveRecord::StatementInvalid。 PG错误

我正在尝试使用Project.find(id)从Project模型中找到一个项目,但它给了我ActiveRecord::StatementInvalid错误完整跟踪-PG::Error:ERROR:preparedstatement"a1"alreadyexists:SELECTCOUNT(*)FROMpg_classcLEFTJOINpg_namespacenONn.oid=c.relnamespaceWHEREc.relkindin('v','r')ANDc.relname=$1ANDn.nspname=ANY(current_schemas(false))/home/deploy

【笔记】internal/modules/cjs/loader.js:985 throw err; ^ Error: Cannot find module ‘node:util‘

[root@localhostusr]#cnpm-vinternal/modules/cjs/loader.js:985throwerr;^Error:Cannotfindmodule‘node:util’Requirestack:/usr/local/node/lib/node_modules/cnpm/bin/cnpmatFunction.Module._resolveFilename(internal/modules/cjs/loader.js:982:15)atFunction.Module._load(internal/modules/cjs/loader.js:864:27)atM

ruby-on-rails - rails : Plus sign in GET-Request replaced by space

在Rails3(Ruby1.9.2)中我发送一个请求StartedGET"/controller/action?path=/41_+"但是参数列表是这样的:{"path"=>"/41_","controller"=>"controller","action"=>"action"}这里出了什么问题?-、*或.符号工作正常,只是+将被空格替换。 最佳答案 这是正常的URL编码,theplussignisashorthandforaspace:Withinthequerystring,theplussignisreservedasshor

ruby-on-rails - 在 ruby​​ 中使用 sort_by(对于 rails)?

所以我构建了一个自定义用户数组,如下所示:[["user1",432],["user1",53],["user9",58],["user5",75],["user3",62]]我想按每个数组中的第2个值从大到小对它们进行排序。我觉得对数组使用sort或sort_by是做到这一点的方法,但我不太确定如何完成它 最佳答案 排序方式如果您对sort_by感兴趣,您可以解构您的内部数组array.sort_by{|_,x|x}.reverse或者调用索引运算符array.sort_by{|x|x[1]}.reverse您可以取反从block

javascript - rake 数据库 :create - Could not find a JavaScript runtime

这是我得到的错误:sergio@sergio-VirtualBox:~/blog$rakedb:createrakeaborted!CouldnotfindaJavaScriptruntime.Seehttps://github.com/sstephenson/execjsforalistofavailableruntimes.(Seefulltracebyrunningtaskwith--trace)sergio@sergio-VirtualBox:~/blog$所以经过一些搜索,我似乎需要为Ruby安装一个Javascript运行时。许多不同的选项之间有区别吗?看来Node.js是

ruby 方式 : catch division by zero

我有以下方法来计算平均值:defcompute_average(a,b,c,d,e)total=[a,b,c,d,e].sum.to_faverage=[a,2*b,3*c,4*d,5*e].sum/totalaverage.round(2)end这没什么特别的,但它有一个问题,我认为所有平均方程都有:如果输入全为零,它可能被零除。所以,我想到了这样做:defcompute_average(a,b,c,d,e)total=[a,b,c,d,e].sum.to_fiftotal==0average=0.00elseaverage=[a,2*b,3*c,4*d,5*e].sum/total

ruby-on-rails - 为 Rails 3.1.1 关闭详细的 sql/ActiveRecord

尽管SQL/ActiveRecord调用的冗长功能在大多数情况下都很有用,但在我有一些循环正在进行的情况下,我想将其关闭。有没有办法关闭它?irb(main):055:0>City.first←[1m←[35mCityLoad(1.0ms)←[0mSELECT`cities`.*FROM`cities`LIMIT1=># 最佳答案 在控制台中:禁用:old_logger=ActiveRecord::Base.loggerActiveRecord::Base.logger=nil启用:ActiveRecord::Base.logger

ruby - Pry Error : Cannot find local context. 您是否使用了 `binding.pry`?

为什么我会收到此pry动错误?[36]pry(main)>s="pry"Error:Cannotfindlocalcontext.Didyouuse`binding.pry`?在此截屏视频中运行良好http://pryrepl.org/ 最佳答案 似乎s、c和n是pry-navgem上的保留命令,发现here,这可以帮助您逐步完成绑定(bind)。Pry.commands.alias_command'c','continue'Pry.commands.alias_command's','step'Pry.commands.alias