Rails Render JSON of a has_many, through Association
<p>I have 3 Models in my application
Artists, Albums and Songs</p>

<pre><code>class Artist &lt; ActiveRecord::Base
    has_many :albums
    has_many :songs, :through =&gt; :albums
end

class Albums &lt; ActiveRecord::Base
    has_many :songs
    belongs_to :artist
end

class Albums &lt; ActiveRecord::Base
    belongs_to :albums
end
</code></pre>

<p>How do render it as JSON wherein it returns an array of the Artists, each artist with a property 'Albums' wherein it is an array of the Albums for each artist and each album has a property Songs which is an array of songs for each album. Like this</p>

<pre><code>[ {
    id: 1,
    name: Artist_Name,
    albums: [
        {
            id: 1,
            name: Album_Name,
            songs: [{
                id: 1,
                title: Song_Title,
                lyrics: Song_Lyrics
            }]
        }
    ]
}]
</code></pre>