首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >返回rails中所有内容的模型方法

返回rails中所有内容的模型方法
EN

Stack Overflow用户
提问于 2013-09-11 04:40:15
回答 1查看 96关注 0票数 0

我要我的方法只返回追随者的数量。我发现,如果它没有进展到最后的' if‘语句,它只返回前面代码中的查询或散列。

代码语言:javascript
复制
def my_twitter_followers
    if JSON.parse(contact_hash)["social_profiles"]
        JSON.parse(contact_hash)["social_profiles"].each do |profile|
            if profile["followers"] 
                return profile["followers"]
            end
        end
    end
end

如果Twitter配置文件存在于包含散列的social_profiles数组中,我希望它返回关注者(这就是为什么下面的“追随者”)

[{"url"=>"http://gravatar.com/xxxxxx“、”id“”=>“xxxxxx”、“键入”=>“Gravatar”、"type_id"=>"gravatar“、"type_name"=>"Gravatar”、“用户名”“=>”xxxxxx“、”BIO“=>”一些大型生物“}、{”url“”=>“http://www.facebook.com/xxxxxxx、”type“”Facebook“、"type_id"=>"facebook”、"type_name"=>"Facebook“、"username"=>"xxxxxx",“id”=>“xxxxx”}、{"url"=>"http://www.quora.com/xxxxxxx“、”键入“=>”Quora“、”用户名“”=>“xxxxx、"type_id"=>"quora”、"type_name"=>"Quora"}、{"url"=>"http://twitter.com/xxxxxxx“、”键入“=>”Twitter“、”用户名“”=>“xxxxx、"type_id"=>"twitter”、“=> "1000”"type_name"=>"Twitter"}{"url"=>"http://plancast.com/user/xxxxxx“、”id“”=>“xxxxxx、”=>“Plancast、"type_id"=>"plancast”、"type_name"=>"Plancast"}、{"url"=>"http://youtube.com/user/xxxxxxx“、”键入“=>”Youtube“、”用户名“”=>“xxxxxx、"type_id"=>"youtube”、"type_name"=>"Youtube"}]

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-09-11 05:28:46

我觉得这个应该管用。如果我真的明白你想要什么。

代码语言:javascript
复制
def my_twitter_followers
    if JSON.parse(contact_hash)["social_profiles"]
        JSON.parse(contact_hash)["social_profiles"].each do |profile|
            if profile["type"] == "twitter" 
                return profile["followers"] unless profile["followers"].blank?
            end
        end
    end
    return 0
end

在这种情况下,如果有一个twitter配置文件。它将返回推特追随者,除非没有追随者。在这种情况下,它将返回0(零)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18732818

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档