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]
}
]
}
}
]
Related
I have this in a controller:
def get_tags
person = Person.where({fuid: params[:fuid]})
render json: person, :only=>[:fuid], :include => [
:hashtags => {
:except => [:created_at, :updated_at],
:methods => :person_count
}
]
end
I am including hashtags but I need to filter those hashtags for only the ones that have active = 1. Is there a way to do this?
You could do this in the model:
class Person < ActiveRecord::Base
has_many :active_hashtags,-> { where active: 1 }, class_name: 'Hashtags'
end
Then in your controller:
render json: person :only=>[:fuid], :include => [
:active_hashtags => {
:except => [:created_at, :updated_at],
:methods => :person_count
}
]
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 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}}}})
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 have three models (simplified here):
class Child < ActiveRecord::Base
has_many :childviews, :dependent => :nullify
has_many :observations, :through => :childviews
end
class Childview < ActiveRecord::Base
belongs_to :observation
belongs_to :child
end
class Observation < ActiveRecord::Base
has_many :childviews, :dependent => :nullify
has_many :children, :through => :childviews
end
I'm sending this to some JavaScript using Rails' to_json method like this:
render :layout => false , :json => #child.to_json(
:include => {
:observations => {
:include => :photos,
:methods => [:key, :title, :subtitle]
}
},
:except => [:password]
)
This works perfectly. Observations are retrieved fine 'through' the join table (childviews).
However, I also want to get at data that sits in the childviews join table; specifically the value for 'needs_edit'.
I can't figure out how to get at this data in a to_json call.
Can anyone help me? Many thanks in advance.
qryss
Not sure, but shouldn't this work?
#child.to_json(
:include => {
:observations => {
:include => :photos,
:methods => [:key, :title, :subtitle]
},
:childviews => { :only => :needs_edit }
},
:except => [:password]
)
EDIT:
This might work too, since childviews belongs_to the overvation:
#child.to_json(
:include => {
:observations => {
:include => { :photos, :childviews => { :only => :needs_edit } }
:methods => [:key, :title, :subtitle]
}
},
:except => [:password]
)
Thanks to Rock for the pointers - I now have it working!
This code:
#child.to_json(:include =>
{
:observations => {
:include => {
:photos => {},
:childviews => {:only => :needs_edit}
},
:methods => [:S3_key, :title, :subtitle]
}
},
:except => [:password]
)
gives me this output (abbreviated for clarity):
{
"child":
{
"foo":"bar",
"observations":
[
{
"foo2":"bar2",
"photos":
[
{
"foo3":"bar3",
}
],
"childviews":
[
{
"needs_edit":true
}
]
}
]
}
}
Thank you, Rock! That was doing my head in.
:)
qryss