0

你好,我正在拼命地渲染包含我包含的嵌套资源中的方法的 json。我尝试了许多变体,但就是无法让那个东西运行。

这就是我所拥有的:

   format.json {render json: @user, :include => [ :votes, :petitions, :roles ] }

这就是我希望的工作

   format.json {render json:  @user, :include => {  
                       :votes => { :methods => [ :status, :count_users_voted ] }, 
                       :petitions => { :methods => [:status, :count_users_voted] }, 
                       :roles 
                       }
               }

任何提示任何人?

4

1 回答 1

0

将这样的内容添加到您的用户模型中:

  def as_json(options = { })
    super((options || { }).merge({
      :methods => [:agrees, :disagrees]
    }))
  end

  def agrees
    self.liked_by_count
  end

  def disagrees
    self.disliked_by_count
  end

很简单,我希望这会有所帮助

于 2013-06-26T07:41:26.157 回答