Rails Object Relationships and Json Rendering

Rails Object Relationships and JSON Rendering

By default you'll only get the JSON that represents modelb in your example above. But, you can tell Rails to include the other related objects as well:

def export
@export_data = ModelA.find(params[:id])
respond_to do |format|
format.html
format.json { render :json => @export_data.to_json(:include => :modelb) }
end
end

You can even tell it to exclude certain fields if you don't want to see them in the export:

render :json => @export_data.to_json(:include => { :modelb => { :except => [:created_at, updated_at]}})

Or, include only certain fields:

render :json => @export_data.to_json(:include => { :modelb => { :only => :name }})

And you can nest those as deeply as you need (let's say that ModelB also has_many ModelC):

render :json => @export_data.to_json(:include => { :modelb => { :include => :modelc }})

If you want to include multiple child model associations, you can do the following:

render :json => @export_data.to_json(include: [:modelA, :modelB, :modelN...])

Rendering a JSON object of a join-model and its associated models

Use JBuilder, it comes by default with Rails. Ignore the lines starting with '#':

Jbuilder.new do |j|

# courses: [{
j.courses <student.courses - replace this with whatever variable> do |course|

# id: <course.id>
j.id course.id

# name: <course.name>
j.name course.name

# students: [{
j.students <course.students - replace with whatever variable> do |student|

# name: <student.name>
j.name student.name

end
# }]

end
# }]

end

Not a lot of code. Removing the comments and simplifying some features, it will look like:

student_courses = <...blah...>
json = Jbuilder.new do |j|
j.courses student_courses do |course|
j.(course, :id, :name)
j.students <course.students - whatever method here>, :name
end
end.target!

Check out their guide, its pretty awesome to generate JSON in plain Ruby DSL. So go ahead and use whatever ruby code you want to fetch students/courses/studentcourses.

Rails render object in json with model associations

I hope this works

def map_locations
@posts = Post.where.not(location: [nil, ""])
render :json => @posts.as_json(:only => [:topic, :location, :latitude, :longitude], :include => {:user => {:only => :user_name}})
end

Please check the apidock for more details.

Rail Object Relationships & JSON Rendering

I would encourage you to use the RABL gem versus overriding as_json for all of your models. It's much easier to define your JSON responses using only the parameters / relationships you want. You can also easily create parent/children nesting.

https://github.com/nesquena/rabl

Returning count of associations instead of all associations themselves

Add a method to you model Object

def properties_count
self.properties.count
end

And on you controller do

render json: @objects.to_json(methods: [:properties_count]) 

Although I am not sure about a possible n+1. In case of that you should use

@objects = Object.includes(:properties).all

Edit
Technically its not intended to give arguments to_json only takes getters. But you could do something along those lines:

class Object
attr_accessor :attribute

def method_with_params(attribute = nil)
attribute ||= attribute
# rest of your code
end
end


@object.attribute = 'foodbar'
render json: @object.to_json(methods: [:method_with_params]

)

When rendering an object in JSON from the controller, how do I include a field from the object's parent?

Try this one

def show

@my_object = MyObject.find(params[:id])
render :json => @my_object.to_json(:include => [:parent => {:include => :address}])

end

You have added include within the include statement,

@my_object.to_json(:include => [:parent, **:include** => :address])

so rails is searching include as one of the method or model.

You can use a array like this for including more than one relationship.

:include => [:parent, :address]


Related Topics



Leave a reply



Submit