怎么做"如果"在模型的数组中每个都在rails中

时间:2014-10-02 14:17:32

标签: ruby-on-rails

有没有办法在模型中询问.each数组是否存在标签或密钥,跳过它或放“0”?

例如,我现在可以从以前的问题中找到它,我想出了问题,但我需要做一个“如果故事[”某事“] [”某事“]不存在,跳过它留下它NIL或者把0“......同一个单一的东西,”如果故事[“某事”}不存在,跳过它留下NIL或0“

这是更新和实时代码......

在我的模特中,我有;

 fbstory = User.current.facebook.get_connection("me", "home")

带回了这个

 => {"id"=>"23648815198_10152713082585199", "from"=>{"category"=>"Sports team", "category_list"=>[{"id"=>"10101672249695001", "name"=>"Sports Team"}], "name"=>"Toronto Maple Leafs", "id"=>"23648815198"}, "to"=>{"data"=>[{"category"=>"Sports league", "name"=>"NHL", "id"=>"61652252465"}]}, "message"=>"From the NHL: David Clarkson looks for a fresh start as he begins his second season with the Blue and White.\n\nRead the full story here: http://is.gd/RfnAuS", "message_tags"=>{"9"=>[{"id"=>"61652252465", "name"=>"NHL", "type"=>"page", "offset"=>9, "length"=>3}]}, "picture"=>"https://fbexternal-a.akamaihd.net/safe_image.php?d=AQCL3fZFMGEP2dJd&w=158&h=158&url=http%3A%2F%2F2.cdn.nhle.com%2Fmapleleafs%2Fimages%2Fupload%2F2013%2F11%2Fclarkson620nov8.jpg", "link"=>"http://mapleleafs.nhl.com/club/news.htm?id=732603", "name"=>"David Clarkson Eager To Turn It Around", "caption"=>"mapleleafs.nhl.com", "description"=>"Right wing David Clarkson did not perform well in his first season with the Toronto Maple Leafs, but he's eager to turn things around in 2014-15.", "icon"=>"https://fbstatic-a.akamaihd.net/rsrc.php/v2/y5/r/sXJx2UP7quc.png", "actions"=>[{"name"=>"Comment", "link"=>"https://www.facebook.com/23648815198/posts/10152713082585199"}, {"name"=>"Like", "link"=>"https://www.facebook.com/23648815198/posts/10152713082585199"}], "privacy"=>{"value"=>""}, "type"=>"link", "status_type"=>"shared_story", "created_time"=>"2014-10-02T13:15:01+0000", "updated_time"=>"2014-10-02T13:16:27+0000", "shares"=>{"count"=>17}, "likes"=>{"data"=>[{"id"=>"10152787182894993", "name"=>"Tracy Walker-Caverson"}, {"id"=>"10152756972054137", "name"=>"Taylor Kathleen"}, {"id"=>"10203569785459900", "name"=>"Robin Eldridge"}, {"id"=>"10152775032829490", "name"=>"Kyle Vachon"}, {"id"=>"10154612226985142", "name"=>"Adam Franks"}, {"id"=>"10152678166165399", "name"=>"Paul G Simons"}, {"id"=>"10152742630043545", "name"=>"Holly Chuback"}, {"id"=>"10201889545160252", "name"=>"Christopher Groeber"}, {"id"=>"302852516569062", "name"=>"Jysha JC"}, {"id"=>"840799135960446", "name"=>"William Deshotel"}, {"id"=>"10154660601695611", "name"=>"Josh Dowden"}, {"id"=>"10152434222598348", "name"=>"Rita Lynn Holland"}, {"id"=>"10152324234392274", "name"=>"Chris Czerny Worton"}, {"id"=>"1499650110286787", "name"=>"Michael Ferguson"}, {"id"=>"10152714836666061", "name"=>"Dean Capstick"}, {"id"=>"375799292570584", "name"=>"Tyler Henry"}, {"id"=>"10152493549483541", "name"=>"Melanie Joy"}, {"id"=>"10153264278549852", "name"=>"Henry Paul Marsh"}, {"id"=>"1481650518753510", "name"=>"Diana Allen"}, {"id"=>"10152462988960369", "name"=>"Matt Calvert"}, {"id"=>"10153285403804782", "name"=>"John George"}, {"id"=>"10152778781075799", "name"=>"Joella Marano"}, {"id"=>"10154644689390263", "name"=>"Dany Yeah Yeah"}, {"id"=>"347021735465740", "name"=>"Yeasha Lee"}, {"id"=>"10152835563632625", "name"=>"Mike Mazzei"}], "paging"=>{"cursors"=>{"after"=>"MTAxNTI4MzU1NjM2MzI2MjU=", "before"=>"MTAxNTI3ODcxODI4OTQ5OTM="}, "next"=>"https://graph.facebook.com/v2.1/23648815198_10152713082585199/likes?access_token=CAAKErEce2PkBAIu2lALgBgZC3IDiZBzjDny8J0eniB4PQNHaTDpzKhlZAo51e7nE3U6qdOvE5qOmLlfOOZCXZAmeyMb2qo9sOlcgrBZCL3Pin5efZBoPFX2R7eeQPj9Uyrs4dZAMLh1MF7chYWln4ZAhM7kU92sU3AS3y9C7PY0WOCsT4Khc05K13juUMqV6cGc9pYYdpSmumCxyXUmNXHnaX&limit=25&after=MTAxNTI4MzU1NjM2MzI2MjU%3D"}}, "comments"=>{"data"=>[{"id"=>"10152713082585199_10152713145555199", "from"=>{"id"=>"10152790777668987", "name"=>"Lucas Neilson"}, "message"=>"if not he will protect the water bottle like a champion.", "can_remove"=>false, "created_time"=>"2014-10-02T13:15:51+0000", "like_count"=>19, "user_likes"=>false}, {"id"=>"10152713082585199_10152713146310199", "from"=>{"id"=>"10152496303744302", "name"=>"Daniel Gertler"}, "message"=>"Let me put it this way, even if Clarkson plays his best he's still getting over paid for far too long.", "can_remove"=>false, "created_time"=>"2014-10-02T13:16:27+0000", "like_count"=>15, "user_likes"=>false}, {"id"=>"10152713082585199_10152713149750199", "from"=>{"id"=>"399195896901819", "name"=>"Jessica Smith"}, "message"=>"Not his fault he was offered the kind of money he was.  He had a bad season last year.  I hope he learns from some of the mistakes he made and has a great season.  He's a hometown guy, let's support him.  Go Leafs Go!", "can_remove"=>false, "created_time"=>"2014-10-02T13:19:16+0000", "like_count"=>11, "user_likes"=>false}, {"id"=>"10152713082585199_10152713170740199", "from"=>{"id"=>"10100208958447874", "name"=>"Jamie Gilbert"}, "message"=>"Package Clarkson and Pheneuf up and send them to Montreal for a bag of pucks.", "can_remove"=>false, "created_time"=>"2014-10-02T13:34:15+0000", "like_count"=>4, "user_likes"=>false}, {"id"=>"10152713082585199_10152713149990199", "from"=>{"id"=>"10152305195902234", "name"=>"Glen Boris"}, "message"=>"Maybe he should stop fighting in f'n preseason", "can_remove"=>false, "created_time"=>"2014-10-02T13:19:31+0000", "like_count"=>3, "user_likes"=>false}, {"id"=>"10152713082585199_10152713146375199", "from"=>{"id"=>"10154660368805434", "name"=>"Jordan Addy"}, "message"=>"Last year was a fluke with suspensions and injuries. Clarkson will bounce back ! 15 goals isn't out of the question from a guy like him ", "can_remove"=>false, "created_time"=>"2014-10-02T13:16:29+0000", "like_count"=>6, "user_likes"=>false}, {"id"=>"10152713082585199_10152713220755199", "from"=>{"id"=>"10152484680833271", "name"=>"Ben Martin"}, "message"=>"Believe it when I see it", "can_remove"=>false, "created_time"=>"2014-10-02T14:07:02+0000", "like_count"=>2, "user_likes"=>false}, {"id"=>"10152713082585199_10152713173640199", "from"=>{"id"=>"10152446658539895", "name"=>"Malcolm Stark"}, "message"=>"All the best David, hope you can turn it around!!", "can_remove"=>false, "created_time"=>"2014-10-02T13:36:12+0000", "like_count"=>2, "user_likes"=>false}, {"id"=>"10152713082585199_10152713201220199", "from"=>{"id"=>"10152800455743092", "name"=>"Kelly Olohan"}, "message"=>"They got him for PP help in front of the net.  Wasn't used in that capacity.  I don't want JVR having to duck when Princess fires those unguided missiles head high.  He'll be better if Carlyle uses him properly.", "can_remove"=>false, "created_time"=>"2014-10-02T13:55:58+0000", "like_count"=>1, "user_likes"=>false}, {"id"=>"10152713082585199_10152713172500199", "from"=>{"id"=>"10204877947046545", "name"=>"Merv McCreight"}, "message"=>"He's not gonna do sh*t! He'll go down in Leafs history as the worst signing that's how Clarkson will be remembered. Even if the Leafs wanted to trade him no one will take on that outrageous contract they gave him, it will be a matter of time when they buy him out.", "can_remove"=>false, "created_time"=>"2014-10-02T13:35:29+0000", "like_count"=>1, "user_likes"=>false}, {"id"=>"10152713082585199_10152713168665199", "from"=>{"id"=>"861347077239287", "name"=>"Doug Crewe"}, "message"=>"you don't have to fight every one david...get your game back first....", "can_remove"=>false, "created_time"=>"2014-10-02T13:32:43+0000", "like_count"=>1, "user_likes"=>false}, {"id"=>"10152713082585199_10152713162890199", "from"=>{"id"=>"803808686308081", "name"=>"Shane Douglas Slaney"}, "message"=>"Shits waste of money like dion", "can_remove"=>false, "created_time"=>"2014-10-02T13:29:20+0000", "like_count"=>1, "user_likes"=>false}, {"id"=>"10152713082585199_10152713157765199", "from"=>{"id"=>"10152303116686394", "name"=>"Greg Lemon"}, "message"=>"He won't do anything different. He STINKS", "can_remove"=>false, "created_time"=>"2014-10-02T13:25:00+0000", "like_count"=>1, "user_likes"=>false}, {"id"=>"10152713082585199_10152713153080199", "from"=>{"id"=>"10154623863695471", "name"=>"Matt Ormrod"}, "message"=>"Eager < Able", "can_remove"=>false, "created_time"=>"2014-10-02T13:21:48+0000", "like_count"=>1, "user_likes"=>false}, {"id"=>"10152713082585199_10152713252205199", "from"=>{"id"=>"735650709839383", "name"=>"Lelik Bolik"}, "message"=>"He is great replacement for Grabo Leafs dumped to make a room\n What a second......", "can_remove"=>false, "created_time"=>"2014-10-02T14:26:31+0000", "like_count"=>0, "user_likes"=>false}, {"id"=>"10152713082585199_10152713241195199", "from"=>{"id"=>"10205162126231921", "name"=>"Duncan McFarlane"}, "message"=>"Matt Bridgeland", "message_tags"=>[{"id"=>"10152704839697829", "name"=>"Matt Bridgeland", "type"=>"user", "offset"=>0, "length"=>15}], "can_remove"=>false, "created_time"=>"2014-10-02T14:20:48+0000", "like_count"=>0, "user_likes"=>false}, {"id"=>"10152713082585199_10152713240700199", "from"=>{"id"=>"305733679628672", "name"=>"Brian Coville"}, "message"=>"Cmon leafs let's go", "can_remove"=>false, "created_time"=>"2014-10-02T14:20:20+0000", "like_count"=>0, "user_likes"=>false}, {"id"=>"10152713082585199_10152713240550199", "from"=>{"id"=>"10152749037064134", "name"=>"Angelo Peroff"}, "message"=>"Has now surpassed the Mike Craig signing as the worst ever by the Leafs.", "can_remove"=>false, "created_time"=>"2014-10-02T14:20:11+0000", "like_count"=>0, "user_likes"=>false}, {"id"=>"10152713082585199_10152713237260199", "from"=>{"id"=>"785791571459178", "name"=>"Max Carbone"}, "message"=>"Andrew Gabriele", "message_tags"=>[{"id"=>"723613634399257", "name"=>"Andrew Gabriele", "type"=>"user", "offset"=>0, "length"=>15}], "can_remove"=>false, "created_time"=>"2014-10-02T14:18:04+0000", "like_count"=>0, "user_likes"=>false}, {"id"=>"10152713082585199_10152713236160199", "from"=>{"id"=>"10154682695180068", "name"=>"Garrett Michael Belisle"}, "message"=>"Get rid of this clown", "can_remove"=>false, "created_time"=>"2014-10-02T14:17:31+0000", "like_count"=>0, "user_likes"=>false}, {"id"=>"10152713082585199_10152713233040199", "from"=>{"id"=>"10152872862792189", "name"=>"Zachary J. Bautista"}, "message"=>"yeah yeah yeah yeah yeah.....", "can_remove"=>false, "created_time"=>"2014-10-02T14:15:25+0000", "like_count"=>0, "user_likes"=>false}, {"id"=>"10152713082585199_10152713228195199", "from"=>{"id"=>"1533228070240049", "name"=>"Tanner Hildebrandt"}, "message"=>"Clarkson can do whatever he wants, he will never be worth the 7 million he's gettin this year. What a joke", "can_remove"=>false, "created_time"=>"2014-10-02T14:11:53+0000", "like_count"=>0, "user_likes"=>false}, {"id"=>"10152713082585199_10152713213730199", "from"=>{"id"=>"1534182693479965", "name"=>"Denis Maurice"}, "message"=>"TRADE", "can_remove"=>false, "created_time"=>"2014-10-02T14:03:20+0000", "like_count"=>0, "user_likes"=>false}, {"id"=>"10152713082585199_10152713213665199", "from"=>{"id"=>"10152753314776052", "name"=>"Bruno Lacaria"}, "message"=>"I see no hope with this project", "can_remove"=>false, "created_time"=>"2014-10-02T14:03:18+0000", "like_count"=>0, "user_likes"=>false}, {"id"=>"10152713082585199_10152713207465199", "from"=>{"id"=>"387959471353097", "name"=>"William Dupuis"}, "message"=>"We look for it too David", "can_remove"=>false, "created_time"=>"2014-10-02T13:59:31+0000", "like_count"=>0, "user_likes"=>false}], "paging"=>{"cursors"=>{"after"=>"MzI=", "before"=>"NTg="}, "next"=>"https://graph.facebook.com/v2.1/23648815198_10152713082585199/comments?access_token=CAAKErEce2PkBAIu2lALgBgZC3IDiZBzjDny8J0eniB4PQNHaTDpzKhlZAo51e7nE3U6qdOvE5qOmLlfOOZCXZAmeyMb2qo9sOlcgrBZCL3Pin5efZBoPFX2R7eeQPj9Uyrs4dZAMLh1MF7chYWln4ZAhM7kU92sU3AS3y9C7PY0WOCsT4Khc05K13juUMqV6cGc9pYYdpSmumCxyXUmNXHnaX&limit=25&after=MzI%3D"}}}

Koala Gem使用Facebook API并返回以上内容..但是我继续为nil获取'undefined method` []':NilClass'错误在下面执行此操作;

 fbstory.each do |story|
    User.current.new_fbtimeline_stories.create({
      fb_id: story["id"], 
      fb_shares: story["share"]["count"], 
    })
 end

我不断收到undefined method [] for nil:NilClass,因为我注意到[有关共享“] [”计数“]或[”分享“],有时不存在什么都没有,所以我在数组中寻找[“分享”],但它不在那里......有些人确实拥有它......

所以我正在寻找一种方法来说“if [”share“]或[”share“] [”count“]确实存在保存值...如果不存在,则跳过它或放0。 ..

希望这能使它成为一个更好的解释。

2 个答案:

答案 0 :(得分:0)

所以我想去了,因为除了创造原因之外我可以做unless?exists?甚至if,并在创建动作之前询问它;

if story.has_key?("shares")
   shares_count = story["shares"]["count"].to_i
else
   shares_count = "0"
end

#then we would go and just continue the create action;

User.current.model.create({
   story_id: story["id"],
   story_shares: shares_count
})

可能是一种肮脏的方式,也许还有很长的路要走,但它到目前为止有效,我希望它能为其他人提供一个更好的主意。

答案 1 :(得分:0)

不是最漂亮的解决方案,但你可以这样做:

fbstory.each do |story|
   User.current.new_fbtimeline_stories.create({
     fb_id: story["id"], 
     fb_shares: story.fetch("share", {}).fetch("count", 0), 
   })
end