草庐IT

find_module

全部标签

ruby-on-rails - ruby rails : Find records without Sorting

我需要按照作为搜索参数传入的准确顺序查找记录。例如,我有一个字符串:item_list="23,12,54,45"通过以下查询,我按“item_list”的asc顺序获取记录-“12,23,45,54”。Inventory.find(item_list.split(","))如何修改上述查询,使其以与“item_list”相同的顺序返回记录。谢谢。 最佳答案 试试这个,虽然它可能只适用于MySQL:Inventory.where("idIN(#{item_list})").order("find_in_set(id,'#{item_

ruby : Could not find RedCloth-4. 2.9

我对此很陌生,我该如何解决这个问题?$rakeCouldnotfindRedCloth-4.2.9inanyofthesourcesRun`bundleinstall`toinstallmissinggems. 最佳答案 您没有RedCloth安装gem就是这样。如果您使用的是Bundlergem,我想您可以输入bundleinstall安装它,否则你可以通过geminstallRedCloth手动安装它. 关于ruby:CouldnotfindRedCloth-4.2.9,我们在Sta

ruby - 类与 Class.new,模块与 Module.new

class之间有什么区别?和Class.new&module和Module.new?我知道:Class.new/Module.new创建一个匿名class/module.当我们第一次将它分配给常量时,它变成了那个class的名称。/module.class/module自动执行此操作。当我们想要继承时,我们可以传递一个参数:Class.new(ancestor).当我们不指定祖先时,它被设置为Object.class使用此语法:classAClass.new返回object.classA返回nil.同样适用于module秒。我错过了什么吗? 最佳答案

ruby-on-rails - rails 4 : how to use OR between conditions on find methods

我的问题类似于thisone,但那里的答案都没有解决我的具体问题。我想找到类似这样的对象:conditions={first_name:@searchORlast_name:@search}Stuff.where(conditions)显然,这种语法是无效的,但这是我能想到的展示我想做的事情的最简单的方法。我想在复杂/复合条件下使用更简洁的哈希语法。我知道你可以用像这样的“纯字符串条件”手写出来Stuff.where("first_name=#{@search}ORlast_name=#{@search}")...但这不是我想知道的。更新看起来您可以对这样的数组执行OR:Stuff.w

ruby - gem 安装失败,出现 "Could not find a valid gem ' yaml'"

我正在从一个当前可用的ruby​​程序构建一个gem。它使用jruby1.7.12,除其他外,它执行“require'yaml”。对于gem,我的Gemfile包含:source'https://rubygems.org'gemspec当我运行时gembuildprogram.gemspec这很好用,但是当我运行时geminstallprogram-0.15.01.gem它失败了ERROR:Couldnotfindavalidgem'yaml'(>=0)inanyrepositoryERROR:Possiblealternatives:aml,cyaml,haml,maml,raml没

ruby-on-rails - rails : Find rows without connection in HABTM relation

我有两个模型,Users和Leads与HABTM关系相关:classLead我现在如何才能只获得那些与用户无关的线索?提前致谢! 最佳答案 您正在寻找的是antijoin.有三种标准方法可以实现这一点,使用空左外连接使用带有NOT和IN关键字的子查询的where子句使用带有NOT和EXISTS关键字的where子句基本上,EXISTS关键字将检查子查询是否返回任何行并将其报告为匹配项,NOT显然会否定真正的匹配项。这是我的首选方式(使用NOT&EXISTS)classUser这是一个使用arel的非SQL方法classUserher

ruby-on-rails - ubuntu 14.04 上 passenger-install-nginx-module 的 Bundler 错误

我正在ubuntu14.04和ruby2.2.4上安装passenger+nginx。passenger-install-nginx-module有bundler错误$passenger-install-nginx-module/home/ubuntu/.rvm/gems/ruby-2.2.4/gems/bundler-1.13.1/lib/bundler/rubygems_ext.rb:45:in`full_gem_path':uninitializedconstantBundler::Plugin::API::Source(NameError)from/home/ubuntu/.r

ruby-on-rails - 为什么 Rails 服务器会出现 "Could not find ' railties 错误?

当我使用railss启动Rails服务器时,出现以下错误:/usr/local/lib/site_ruby/1.8/rubygems/dependency.rb:296:in`to_specs':Couldnotfind'railties'(>=0)among10totalgem(s)(Gem::LoadError)from/usr/local/lib/site_ruby/1.8/rubygems/dependency.rb:307:in`to_spec'from/usr/local/lib/site_ruby/1.8/rubygems/core_ext/kernel_gem.rb:4

ruby - 欧拉计划 1 :Find the sum of all the multiples of 3 or 5 below 1000

我正在尝试使用ProjectEuler中的Ruby解决数学问题。Here是我尝试的第一个:Ifwelistallthenaturalnumbersbelow10thataremultiplesof3or5,weget3,5,6and9.Thesumofthesemultiplesis23.Findthesumofallthemultiplesof3or5below1000.请帮助我改进我的代码。total=0(0...1000).eachdo|i|total+=iif(i%3==0||i%5==0)endputstotal 最佳答案

ruby-on-rails - 不区分大小写的 find_or_create_by_whatever

我希望能够执行Artist.case_insensitive_find_or_create_by_name(artist_name)[1](并使其在sqlite和postgreSQL上都有效)实现此目标的最佳方法是什么?现在我只是直接向Artist类添加一个方法(有点难看,特别是如果我想在另一个类中使用此功能,但无论如何):defself.case_insensitive_find_or_create_by_name(name)first(:conditions=>['UPPER(name)=UPPER(?)',name])||create(:name=>name)end[1]:嗯,理