現在我試圖調用我的模型,並將其結果吐出爲json/xml格式。我唯一的問題是,我的數據庫關聯沒有被加載或查詢。在查找中預加載模型關聯
通常情況下,我可以只運行此
@campaign = Campaign.find(:all)
然後調用,@campaign[0].hits
通過has_many :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> </campaign>
然後有它,而不是添加另一列,但他witht點擊率。
<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>
那做什麼,我問,但有可能不返回行,但只算來,並返回整數? – Garrett 2009-01-27 22:31:33