Ruby on rails Rails-使用JBuilder构建自定义JSON

Ruby on rails Rails-使用JBuilder构建自定义JSON,ruby-on-rails,json,jbuilder,Ruby On Rails,Json,Jbuilder,我目前正在从事一个个人Rails 4项目,在其中一个控制器中,我有一个包含三个不同对象的数组,我正试图将其转换为JSON。我使用find_by_sql方法通过自定义MySQL查询数据库。代码如下: 控制员: 类活动控制器

我目前正在从事一个个人Rails 4项目,在其中一个控制器中,我有一个包含三个不同对象的数组,我正试图将其转换为JSON。我使用find_by_sql方法通过自定义MySQL查询数据库。代码如下:

控制员:

类活动控制器<应用程序控制器 def索引 用户=使用设备的当前用户id 极限=100 likes=Like.find\u by\u sql。。。 共享=共享。通过\u sql查找\u。。。 comments=Comment.find\u by\u sql。。。 @活动=[喜欢、分享、评论] 终止 终止 三个对象中的每一个都包含不同的字段。例如,likes对象包含post_id、first_name、last_name等字段。shares对象包含post_id、share_count、shared_time等字段

这是我想要输出的JSON:

{ 喜欢:[ {post_id:7,first_name:Yss,last_name:Salas,profile_pic:345943754_o.png}, {post_id:34,名字:Jessica,姓氏:Nigri,profile_pic:pikachu.png} ], 股份:[ {发布id:43,共享计数:54,共享时间:2014-05-04 15:14:45}, {发布id:54,共享计数:17,共享时间:2014-05-24 03:43:45} ], 评论:[ {post\u id:34,first\u name:Yss,last\u name:Salas,comment:“Me gustas mucho!”, {post_id:12,名字:珍娜,姓氏:大理石,评论:诸如此类诸如此类看看我的丑狗啊 ] } index.json.jbuilder:

json.array@活动做|子活动| json.array!子活动不做|活动| 如果activity.class==Like 此行中引发了json.likes.post_id activity.post_id异常 json.likes.title活动.title json.likes.first_name活动.first_name json.likes.last_name活动.last_name json.likes.profile_pic activity.profile_pic elsif activity.class==共享 json.shares.post_id activity.post_id json.shares.share\u count activity.share\u count json.shares.shared_time活动。shared_time 其他的 json.comments.post_id activity.post_id json.comments.first_name活动.first_name json.comments.last_name活动.last_name json.comments.comment activity.comment 终止 终止 终止
上面在Activitiesindex中引发了一个异常NoMethodError,它是的未定义方法“post_id”。我对Rails非常陌生,我猜这是因为没有json.likes属性来分配值。如何使用JBuilder构建上述JSON结构?

不使用JBuilder,您可以更轻松、更快地完成这项工作

def index
  user = current_user.id # using devise
  limit = 100

  likes = Like.find_by_sql(...).map(&:attributes) # this will return an array of JSON objects
  shares = Share.find_by_sql(...).map(&:attributes)
  comments = Comment.find_by_sql(...).map(&:attributes)

  @activities = {"likes" => likes, "shares" => shares, "comments" => comments}
end
像这样,你就有了一个包含所有你需要的信息的散列。
最好将查找喜欢、共享和评论的逻辑提取到一个单独的类中,这样您就可以调用InformationCollector.collect,它将为您提供所需的响应。

无需JBuilder,您就可以轻松、快速地完成这项工作

def index
  user = current_user.id # using devise
  limit = 100

  likes = Like.find_by_sql(...).map(&:attributes) # this will return an array of JSON objects
  shares = Share.find_by_sql(...).map(&:attributes)
  comments = Comment.find_by_sql(...).map(&:attributes)

  @activities = {"likes" => likes, "shares" => shares, "comments" => comments}
end
像这样,你就有了一个包含所有你需要的信息的散列。 最好将查找喜欢的、共享的和评论的逻辑提取到一个单独的类中,这样您就可以调用InformationCollector.collect,它将为您提供所需的响应。

从Jbuilder的,解决方案相当简单。所有需要做的就是将实例变量传递到一个块中,并手动分配自定义键/值对

下面的示例将为@likes实例变量生成一个键/值对数组:

活动/index.json.jbuilder json.likes@likes do | like| json.post\u id like.post\u id json.first\u name like.user.first\u name json.last_name like.user.last_name json.profile_pic like.user.profile_pic 终止 以上将输出:

{
  "likes": [
    { "post_id": 7, "first_name": "Yss", "last_name": "Salas", "profile_pic": "345943754_o.png" },
    { "post_id": 34, "first_name": "Jessica", "last_name": "Nigri", "profile_pic": "pikachu.png" }
  ],
}
从Jbuilder看来,解决方案相当简单。所有需要做的就是将实例变量传递到一个块中,并手动分配自定义键/值对

下面的示例将为@likes实例变量生成一个键/值对数组:

活动/index.json.jbuilder json.likes@likes do | like| json.post\u id like.post\u id json.first\u name like.user.first\u name json.last_name like.user.last_name json.profile_pic like.user.profile_pic 终止 以上将输出:

{
  "likes": [
    { "post_id": 7, "first_name": "Yss", "last_name": "Salas", "profile_pic": "345943754_o.png" },
    { "post_id": 34, "first_name": "Jessica", "last_name": "Nigri", "profile_pic": "pikachu.png" }
  ],
}

虽然您的示例很方便,但不幸的是,我需要在这个项目中使用Jbuilder。我已经弄明白了,我将在项目的GitHub页面上的JBuilderWiki上回答我自己的问题。看起来我想得太多了,解决方案比我想象的要简单得多。虽然你的例子很方便,但不幸的是,我需要在这个项目中使用Jbuilder。我已经弄明白了,我将在项目的GitHub页面上的JBuilderWiki上回答我自己的问题。看来我想得太多了,解决办法比我想象的要简单得多。