草庐IT

first-child

全部标签

ruby-on-rails - 根据 child 的创建日期对对象进行排序

我正在用Rails创建一个基本论坛。我有一个有很多主题的论坛,主题有很多帖子。我想获取特定论坛的所有主题,并以第一个是具有最新帖子的主题的方式对它们进行排序。在我的帖子存储库中,我有一个方法可以返回最新的帖子。deflatest_postorder('created_atDESC').limit(1).firstend在Threads类中,我有一个方法调用latest_post返回最新的帖子deflatest_postself.posts.latest_postend我按以下方式使用该方法来获取线程的最新帖子。@some_thread.posts.latest_post现在我想获取线程

ruby - 使用 'first_or_create' 时应该如何合并哈希

我有这个哈希,它是动态构建的:additional_values={"grouping_id"=>1}我想在通过first_or_create创建后将其与此记录对象合并:result=model.where(name:'test').first_or_createdo|record|#I'mtryingtomergeanyrecordattributesthatexistinmyhash:record.attributes.merge(additional_values)#Thisworks,butitsucks:#record.grouping_id=data['grouping_i

ruby - 如何: Ruby Range that doesn't include the first value

使用Ruby中的范围,您可以执行0..5来包含0和5之间的所有数字(包括0和5)。您还可以执行0...5来包含不包括除5以外的相同数字。(1..5)===5=>true(1...5)===5=>false(1...5)===4.999999=>true有没有办法排除第一个数字而不是最后一个数字以获得这样的结果?(1...5)===1=>false(1...5)===1.00000001=>true 最佳答案 不,没有对此类范围的内置支持。如果此行为是必要的,您可能希望推出自己的Range类。

ruby - YAML/ ruby : Get the first item whose <field> is <value>?

我有这个YAML:-company:-id:toyota-fullname:トヨタ自動車株式会社-company:-id:konami-fullname:KonamiCorporation而且我想获取ID为konami的公司的全名。使用Ruby1.9.2,最简单/常用的获取方式是什么?注意:在我的其余代码中,我一直在使用require"yaml",所以我更愿意使用相同的库。 最佳答案 这也行,但不使用迭代:y=YAML.load_file('japanese_companies.yml')result=y.select{|x|x['

ruby-on-rails - Rails 上的 Ruby + Active_Record : How do I include child counts in result?

我想出了如何通过在文件夹模型中创建as_json方法将子项包含在to_json结果中。defas_json(options={})super(options.merge(:include=>{:children=>{}}))end上面的代码给了我一个child的列表,但我想要的是包括计数而不是child的列表。我还想将其过滤为仅“活跃”的child。我似乎想不出一个有效的方法来做到这一点。我正在使用以下代码返回文件夹列表。defindex@folders=Folder.all(:order=>"Name")respond_with(@folders)do|format|format.j

ruby-on-rails - rails : get the first n active record objects of a model

我需要一种方法来获取模型的前n项。Item.first(n)、Item.all[1..n]会这样做,只是它们返回的是数组,而不是对象。如何将其作为ActiveRecord对象获取?irb(main):135:0>Player.where(game_id:1).class=>Player::ActiveRecord_Relation#Okirb(main):136:0>Game.first.players.class=>Player::ActiveRecord_Associations_CollectionProxy#Okirb(main):137:0>Player.where(game

ruby 可枚举 : first truthy value of a block

在ruby​​中我们可以这样做:stuff_in_trash.detect(&:eatable?)=>:pack_of_peanutsstuff_in_trash.detect(&:drinkable?)=>nil但是,如果我们对block第一次为真时的值感兴趣,而不是block为其取真值的第一个项目感兴趣怎么办?也就是转换如下代码:deftry_to_make_artwork_from(enumerable)enumerable.eachdo|item|result=make_artwork_fromitemreturnresultifresultendnilend类似于:deftr

Ruby 枚举 : Taken first n where block returns true

我想取前“n”个通过该block的条目a=1..100_000_000#Basicallyalongarray#Thisiteratesoverthewholearray--nogoodb=a.select{|x|x.expensive_operation?}.take(n)一旦我得到n个“昂贵”条件为真的条目,我想缩短迭代。你有什么建议?take_while并保持计数n?#Thisisthecodeihave;whichithinkcanbewrittenbetter,buthow?a=1..100_000_000#Basicallyalongarrayn=20i=0b=a.take

ruby - 找不到 child_model (NameError)

我无法确定为什么会出现名称错误。我是DataMapper的新手,但正在尝试减少关联。感谢您的帮助。用户:classUserincludeDataMapper::Resourceproperty:id,Serial,:key=>trueproperty:first_name,Stringproperty:last_name,Stringproperty:company,Stringproperty:city,Stringproperty:country,Stringproperty:mobile_number,Integerproperty:email_address,Stringpro

ruby 哈希 : return the first key value under which is not nil

假设我有一个散列hash={a:1,b:false,c:nil}&某处的一系列键:[:c,:b,:a]。在!=nil下返回这样的键值是否有Ruby惯用语?目标[:c,:b,:a].select{|key|hash[key]!=nil}.first#returns:b似乎太长了。 最佳答案 我认为Enumerable#find可能有效:find(ifnone=nil){|obj|block}→objornilfind(ifnone=nil)→an_enumeratorPasseseachentryinenumtoblock.Retur