2011-01-30 67 views
0

我想呈現一個JSON對象包含商店(模型:商店)的列表。用戶與商店有關聯,但是每個用戶在每個商店都有唯一的可用點數供使用。找點可用於商店或總數的表達式爲:Rails活動記錄字段,動態值(評估)

@user.codes.unspent.find_all_by_store_id(@store.id).count 
# unspent is a scope 
# @user is the currently logged in user 
# Code model keeps all activity records of points. 

因爲我使所有門店的JSON對象列表,上面表達顯然不是在商店數據庫中的字段,如何我可以評估這個表達式並返回每個商店的計數嗎?

此外,什麼是性能影響?

謝謝。

+0

你應該呈現自己的js.erb文件 – apneadiving 2011-01-30 21:13:25

回答

1

也許這可以幫助,從apidocs

@events.to_json(:include => { 
        :images => { 
         :only => [], :methods => [:public_url] }}) 

它填充自定義方法的JSON。

+0

這是在正確的軌道,但我似乎無法將正確的變量傳遞給我放在Store模型中的方法:我做了@user = current_user,然後調用@user在方法中,但我得到:無類。 – JBlake 2011-01-30 22:54:43