Correction: You can do something like
render json: @teams.to_json(:except => [:created_at, :updated_at], :include => { :stadiums => { :except => [:created_at, :updated_at]}, ... })
There is no easy way to do this without iterating over the appropriate models, obtaining attribute hashes, and selecting the desired attributes.
Such use cases are often resolved elegantly using json templating DSLs such as jbuilder or rabl .
To illustrate this with jbuilder:
Jbuilder.encode do |json|
json.array! @teams do |team|
json.name team.name
json.stadiums team.stadiums do |stadium|
json.name stadium.name
end
end
end
Which would give an output like:
[{
name: "someteamname",
stadiums: {
name: "stadiumname"
},
...
}, {...},...]
, @liamneesonsarmsauce, - ActiveModel
, serializer , , json-.
class TeamSerializer < ActiveModel::Serializer
attributes :id, :name
has_many :stadiums
has_many :scores
has_many :links
has_many :rounds
end
.
, rails, json, .