include 2nd level in to_json - ruby-on-rails

I'm using this code to convert a model to json. If i try to use an include 2nd level like this:
p = Product.includes({ :variants => { :stocks => :size } }).where(:id => params[:id]).first
render :json => p.variants.to_json(:include => { :stocks => { :include => :size } })
I receive this error:
undefined method `macro' for nil:NilClass
How I can solve that?

Try this:
render :json => p.variants.map { |v| v.as_json(:include => {:stocks => {:include => :size}}) }
Info about Object#as_json/to_json here.

Related

Ruby on Rails render array of objects plus number of elements

I have a rails method with this render:
render :json => #boats, :include => { :mainPhoto => {:only => [:id, :mime]},
:boat_model => { :only => :name, :include => {:boat_type => { :only => :name}}}}
Can I include a numTotal variable in this response?
You'll need to use the as_json method to serialize #boats in separated JSON key:
render :json => { numTotal: boats.size, boats: #boats.as_json(:include => { :mainPhoto => {:only => [:id, :mime]},
:boat_model => { :only => :name, :include => {:boat_type => { :only => :name}}}})

inner methods on attributes in respond_with

I'm rendering a serialized JSON response with the following code:
respond_with(#posts, :only => [:id, :content, :created_at],
:include => { :user => { :only => [:id, :name] },
:comments => { :only => [:content, :created_at] }})
the response is parsed in JAVA code so I want to convert the created_at value to a format that I can use, how do i run a method on each created_at value (even .to_i for example) ?
I would do it defining a new method inside comments model e.g.
class Comment < ActiveRecode::Base
...
def created_at_to_i
created_at.to_i
end
...
end
and while rendering
respond_with(#posts, :only => [:id, :content, :created_at],
:include => { :user => { :only => [:id, :name] },
:comments => { :only => [:content, :created_at_to_i] }})
Define method in each model like :
def formated_created_at
self.created_at.strftime(FORMAT_AS_YOU_LIKE)
end
At the time of rendering use it like :
respond_with(#posts, :only => [:id, :content, :formated_created_at
,
:include => { :user => { :only => [:id, :name] }, :comments => { :only => [:content, :formated_created_at
] }})
EDITED :
Instead of using the :only in respond_with , you can create your own hash and pass it to respond_with .
Like :
post_hash= { :post => [{:id => 1, :content => "abc", :created_at => self.formated_created_at,:user => {:id => 1, :name => 'Vik' }, :comments => { :content => "comment text", :created_at => self.comment.formated_created_at}] }
respond_with(post_hash)
And I think u can format the created_at at the time of displaying , via javascript, jQuery .

possible to nest method calls on to_json

I'm trying to do something like this:
render :json => r.to_json(:methods => [:food_item => {:method => :price_value}])
but it's not working. Is something like this even possible?
thx
edit 1
no association
def food_item
MenuItem.find(food_id)
end
Is food_item an ActiveRecord association? If so, you could try
render :json => r.to_json(:include => { :food_item => { :only => :price_value } })
I'll refine my answer in response to "edit 1". First, remove your food_item method and add an actual association like this:
belongs_to :food_item, :class_name => "MenuItem", :foreign_key => "food_id"
and then do
render :json => r.to_json(:include => { :food_item => { :only => [:price_value] } })

Rails 3.1 deep nesting with RABL

I'm using the RABL gem to render JSON user data for users of comments which are children of annotations which are children of images. I'd like to do something similar to:
object #image
node :annotations do
#image.annotations.map { |a| {
:id => a.id,
:x1 => a.x1,
:x2 => a.x2,
:y1 => a.y1,
:y2 => a.y2,
node :comments do
#image.annotations.comments.map { |c| {
:body => c.body,
:user_id => c.user_id,
:name => User.find(c.user_id).name,
:user_icon => user_icon(User.find(c.user_id), 'square', 30)
}}
end
}}
end
I know this isn't valid in RABL, I also tried using child instead of node, but couldn't access the user data that way. How should I go about doing this and whats the proper syntax to make this happen?
I got this answer from #calvin-l. The trick was to just map the a.comments then grab the data from each comment that way:
node :annotations do
#image.annotations.map { |a| {
:id => a.id,
:x1 => a.x1,
:x2 => a.x2,
:y1 => a.y1,
:y2 => a.y2,
:comments => a.comments.map { |c| {
:body => c.body,
:created_at => c.created_at,
:user => {
:id => c.user.id,
:facebook_id => c.user.facebook_id,
:name => c.user.name,
:username => c.user.username
}
}}
}}
end

Rails - Is having an EACH loop inside a hash doable somehow?

#items.each_with_index do |item, i|
#mylist << {
:id => item.id,
:name => name.id,
:users => {
item.participations.each do |participant|
:participant_image_link => participant.imagelink
end
}
}
end
render :json => { :result => 'success', :mylist => #mylist }
I need the ability for an array for :users but can't figure out how to build it without breaking Rails.
Thanks
:users => item.participants.map { |participant|
{ :participant_image_link => participant.imagelink }
}

Resources