2011-01-30 58 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