samedi 5 mars 2016

rails how to serialize ?

I've this problem, i wan't to return json after somthing logic.

  def get_comments_by_place
      comments = PlaceComment.where(place_id: params[:place_id].to_i)
      arr = {}
      arr[:last_page] = false
      arr[:data] = comments
      respond_to do |format|
        format.json { render json: arr, root: false }
      end
  end

also i have serializers

PlaceComment serializer:

class PlaceCommentSerializer < ActiveModel::Serializer
  attributes :id, :user, :human_created_at, :comment
end

also i have next relations: user has_many PlaceComments place_comment belongs_to user

json is:

{"last_page":true,"data":[{"id":5,"event_id":58,"user_id":1,"comment":"hello","created_at":"2016-03-03T00:21:47.000Z","updated_at":"2016-03-03T00:21:47.000Z"}]}

So i wan't to get also User object on my json.

How i can do it?

Aucun commentaire:

Enregistrer un commentaire