の後Post.all
、カスタム属性を持つ投稿オブジェクトの JSON 配列を返そうとしています。ここに私が持っているものがあります:
@posts = Post.includes(:profil).page(params[:page]).order('created_at DESC')
render json: @posts.to_json(:include => :profil,
:methods => [:image_url, :vote_modificator])
Profil
このメソッドは、カスタム属性を含む投稿のリストを正しく返しますimage_url
。Vote
およびに属するモデルがProfil
ありPost
ます。
class Vote < ActiveRecord::Base
attr_accessible :value
belongs_to :profil
belongs_to :post
end
Vote
各投稿に、それ自体に対応する値と、そのcurrent_user
ように定義された値を挿入したいapplication_controller.rb
:
def current_user
@current_user ||= User.find_by_auth_token!(cookies[:auth_token]) if cookies[:auth_token]
end
helper_method :current_user
にメソッドを追加しようとしましたto_json
が、モデルで定義されたメソッドはこのヘルパーにアクセスできません。私の問題を解決するための解決策はありますか?