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
}
]
Related
I am facing this error while running my application:
<ActiveRecord::Associations::CollectionProxy []>
I am able to store reports but couldn't store icons. It is storing fine in Rails 3.2.13, but raising this issue in Rails 4.2.6.
report.rb:
class Report < ActiveRecord::Base
belongs_to :user
has_many :icons, -> { order 'position_id ASC'}
accepts_nested_attributes_for :icons, :reject_if => lambda { |a| a[:icon].blank? }, :allow_destroy => true
end
icon.rb:
class Icon < ActiveRecord::Base
belongs_to :report
end
reports_controller:
def new
#report = #user.reports.new({
:background_color => Rails.application.config.custom.accounts.send(#user.account.name).colors.background,
:text_color => Rails.application.config.custom.accounts.send(#user.account.name).colors.commentary,
:button_color => Rails.application.config.custom.accounts.send(#user.account.name).colors.button
})
3.times { #report.icons.build }
end
def create
respond_to do |format|
if #report.save
format.json { render :json => { :success => true, :user_id => #user.id, :report_id => #report.id, :report_title => #report.title, :icon_array => #report.icons, :redirect => user_report_url(current_user, #report.id) } }
else
format.json { render :json => { :success => false } }
end
end
end
I am able to store reports but icons are not stored. Please help
I think you might have missed icon attributes in the strong parameters.
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 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 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