Right now I am trying to call my model, and spit out its results into json/xml format. My only problem is, my database associations aren't being loaded or queried.
Normally, I can just run this
@campaign = Campaign.find(:all)
Then get the number of hits by calling, @campaign[0].hits
through the has_many :hits
.
But if you debug the output, it only calls the columns on the table. How would you go about having it put it alongside your query?
In example:
<campaign> <category>website</category> <created-at type="timestamp">2009-01-24 14:49:02 -0800</created-at> <end-at type="date">2009-01-24</end-at> <id type="integer">14</id> <is-watched type="integer">1</is-watched> <name>Lets</name> <slug>c5334415da5c89384e42ce6d72609dda</slug> <start-at type="date">2009-01-24</start-at> <user-id type="integer">5</user-id> </campaign>
Then having it instead add another column, but witht he number of hits.
<campaign> <category>website</category> <created-at type="timestamp">2009-01-24 14:49:02 -0800</created-at> <end-at type="date">2009-01-24</end-at> <id type="integer">14</id> <is-watched type="integer">1</is-watched> <name>Lets</name> <slug>c5334415da5c89384e42ce6d72609dda</slug> <start-at type="date">2009-01-24</start-at> <user-id type="integer">5</user-id> <hits type="integer">123412</hits> </campaign>
The ActiveRecord find() family takes an :include option which allows you to eager load your associatons.
So all you need to do is:
@campaign = Campaign.find(:all, :include => :hits)
The above will eager load your database calls so that accessing each index [0]. [1], etc wont issue their own SELECT calls. When you call
@campaign[0].to_json
Then it will NOT include any associations, such as "hits" to do that then you also need to :include it on the to_json call, e.g.
@campaign[0].to_json(:include => :hits)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With