草庐IT

find_or_build_by

全部标签

sql - GROUP BY 和 COUNT 使用 ActiveRecord

引用这个:IsthereanydifferencebetweenGROUPBYandDISTINCTGivenatablethatlookslikethis:name------barrydavebilldavedavebarryjohnThisquery:SELECTname,count(*)AScountFROMtableGROUPBYname;Willproduceoutputlikethis:namecount-------------barry2dave3bill1john1对于ActiveModel使用COUNT执行GROUPBY的正确Rails约定是什么?

ruby - "find_all"和 "select"是一回事吗?

这两个语句给我相同的结果:[1,2,3,4].find_all{|x|x.even?}[1,2,3,4].select{|x|x.even?}find_all只是一个别名吗?有理由使用一个而不是另一个吗? 最佳答案 #find_all和#select非常相似;差异非常微妙。在大多数情况下,它们是等价的。这取决于实现它的类。Enumerable#find_all和Enumerable#select在同一代码上运行。Array和Range也是如此,因为它们使用Enumerable实现。在Hash的情况下,#select被重新定义为返回一

ruby sort_by 多个字段

我正在运行Ruby1.9.3p392。Item=Struct.new(:name,:dir,:sort_dir)entries=ftp.list()entries.map!{|e|Net::FTP::List.parse(e)}.map!{|e|Item.new(e.basename,e.dir?,(e.dir??0:1))}renderjson:entries.sort_by{|e|[e.sort_dir,e.name]}出于某种原因,我没有得到预期的结果。我确实首先获取了所有文件夹,然后是所有文件,但是,名称排序失败。例如,我为我的文件夹获取了这些:内容图片本对于文件:全局.asa

ruby-on-rails - 如何在 ruby​​ 中使用 sort_by 按字母顺序对数组进行排序?

我有一组成员资格。每个成员中都有一个组。我需要按组名对这个成员资格数组进行排序。我尝试了很多不同的方法,最新的方法是这样的:@memberships.sort_by!{|m|m.group.name}但是,这不按名称排序。它似乎是对数组进行随机排序。成员属于:组组has_many:memberships@memberships等于:[{id:2141,user_id:491,group_id:271,member_type:"member",group:{id:271,name:"Derek's",privacy:"open",bio_image_url:"/bio_images/me

ruby-on-rails - 为什么 Rspec 说 "Failure/Error: Unable to find matching line from backtrace"?

我在这里学习Rails教程:http://railstutorial.org/chapters/filling-in-the-layout#top当我运行“rspecspec/”时,我得到一堆如下所示的错误:1)LayoutLinksshouldhaveaHomepageat'/'Failure/Error:Unabletofindmatchinglinefrombacktracestackleveltoodeep#C:/Ruby19/lib/ruby/1.9.1/forwardable.rb:1852)LayoutLinksshouldhaveaContactpageat'/cont

ruby - 轨道 3/ ruby : ActiveRecord Find method IN condition Array to Parameters single quote issue

我在微博模型上创建了一个方法,它接受一个user_id数组。在此方法中,我使用以下“查找”方法来提取数组中所有用户的所有帖子。find(:all,:conditions=>["user_idIN(?)",args.join(',')])但是当ActiveRecord为这个查询生成SQL时,它用单引号将逗号分隔的ID列表括起来。这会导致查询只提取单引号内第一个数字的帖子,而不是所有数字。SELECT`microposts`.*FROM`microposts`WHERE(user_idIN('3,4,5'))ORDERBYmicroposts.created_atDESC查询应该看起来像这

ruby-on-rails - 错误 : failed to build gem native extension when installing rails on mac mountian lion os

我最近更新到MountainLion并重新安装了Ruby,但是当我尝试运行测试Rails应用程序时,我收到一条错误消息,指出“我的系统当前未安装Rails”。我按照它说的做,输入sudogeminstallrails并得到:clearedfaster_requirecachesduetonewgeminstall...Successfullyinstalledrails-3.2.71geminstalledInstallingridocumentationforrails-3.2.7...InstallingRDocdocumentationforrails-3.2.7...但是当我检

ruby 和 "You must recompile Ruby with OpenSSL support or change the sources in your Gemfile"

我使用rvm将我的ruby​​升级到1.9.3-p392,还添加了2.0.0,每当我尝试使用这个版本时,当我运行我的bundle命令时,我都会收到这个错误。CouldnotloadOpenSSL.YoumustrecompileRubywithOpenSSLsupportorchangethesourcesinyourGemfilefrom'https'to'http'.InstructionsforcompilingwithOpenSSLusingRVMareavailableatrvm.io/packages/openssl.我已经按照几个不同的说明来解决这个问题。我尝试删除版本并

ruby-on-rails - macOS, rails : "Failed to build gem native extension"

我无法尝试在我的Mac上安装Rails。我有OSX10.6.8,我已经确认我有Ruby,版本1.8.7我运行了sudogemupdate和sudogemupdate--system来获取最新版本的软件。但是,当我运行sudogeminstallrails时,出现了这个错误:ERROR:Errorinstallingrails:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/rubyextconf.rbmkmf.rbcan'tfindh

ruby-on-rails - 使用 'gem pq' 安装 PostgreSQL gem 失败并出现错误 : Failed to build gem native extension

我正在学习RubyonRails并尝试开发应用程序。在我的应用程序中,我试图在开发模式下使用默认的SQLite数据库,在生产模式下使用PostgreSQL。但是我在尝试使用安装pggem时遇到以下错误:geminstallpgBuilding native extensions.  This could take a while...ERROR:  Error installing pg:        ERROR: Failed to build gem native extension.     /home/tusharkhatiwada/.rvm/rubies/ruby-2.0.