Reputation: 107
I am using Rails 3.2.12.
I have a model A
and table called a
. I need to tag in a_details
which is a set of attributes based on type of A
. There is no table for a_details
. Its made on the fly. In the show method I'm calling a method getAdetails
and in the response I am passing both as JSON objects, A
and A_details
.
This gets tricky in the index
method as there is going to be many and I don't think I want to loop through each one to make an array object and render them through. I was thinking about table-less model and using a gem called activerecord_association
using the :include
method while rendering to JSON. I was thinking, is there a better way of doing this?
# show method.
a = A.new
@a_detail = @a.getADetails
response = {:notification => @a.as_json(:include => :creator), :a_detail => @a_detail}
#index
respond_to do |format|
format.html # index.html.erb
format.json { render json: @objects.as_json(:include => :creator) }
end
Upvotes: 1
Views: 191
Reputation: 8586
How about using the :methods option?
render json: @objects.as_json(include:'creator', methods:'getADetails')
From http://api.rubyonrails.org/classes/ActiveModel/Serializers/JSON.html#method-i-as_json
Edited to return an array
Upvotes: 1