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}}}})
Related
I return all information in a scream as JSON.
I want to return how long ago it was created.
include ActionView::Helpers::DateHelper
def as_json(options={})
super(:only => [:id, :yell_type, :status, :payment_type],
:include => {
:trade_offer => {:only => [:id, :title, :description, :price],
:include => [:photos => {:only => [:id, :url]}]
},
:categories => {:only => [:id, :name]},
:user => {:only => [:id, :name, :avatar]}
},
:methods => [ times_ago(:create_at) ]
)
end
def times_ago(create_at)
time_ago_in_words(create_at)
end
This returns an error:
comparison of Symbol with Time failed
How should I do that?
You can add methods on the same level as include and only. So the return value of the method will be passed in the JSON too. In this case, you should implement a method times_ago in the model that returns what you want.
def as_json(options={})
super(
:only => [:id, :yell_type, :status, :payment_type],
:include => {
:trade_offer => {:only => [:id, :title, :description, :price],
:include => [:photos => {:only => [:id, :url]}]
},
:categories => {:only => [:id, :name]},
:user => {:only => [:id, :name, :avatar]}
},
:methods: [ :times_ago ]
)
end
I am attempting to nest some as_json includes. However, only the first shows up. My code is below. In the below case, only the product is rendered. However, if I switch the order of order_pad and product, only the order_pad renders.
#Renders json
def as_json(options={})
super(
:except => [:created_at, :updated_at, :order_pad_id, :product_id],
:include => [
:product => {
:except => [:created_at, :updated_at]
},
:order_pad => {
:except => [:created_at, :updated_at, :user_id],
:include => [
:user => {
:only => [:id, :name_first, :name_last, :company]
}
]
}
]
)
end
You need to send those as two separate hashes:
:include => [
{
:product => {
:except => [:created_at, :updated_at]
}
},
{
:order_pad => {
:except => [:created_at, :updated_at, :user_id],
:include => [
:user => {
:only => [:id, :name_first, :name_last, :company]
}
]
}
}
]
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 .
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] } })
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.