Rails Active Record 字段,动态值(评估)
我正在尝试呈现一个包含商店列表(模型:商店)的 JSON 对象。用户与商店有关联,但是每个用户在每个商店都有唯一的可用积分数。查找商店可用积分总数的表达式为:
@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 对象列表,并且上面的表达式显然不是商店数据库中的字段,所以我如何计算该表达式并返回每个商店的数量?
另外,对性能有何影响?
谢谢。
I'm trying to render a JSON object containing a list of Stores (Model: Store). Users have associations to Stores, however each User has a unique # of points available at each store for use. The expression to find the total count of points available for a store is :
@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.
Because I am rendering a JSON object list of all the Stores, and the above expression is obviously not a field in the Store database, how can I evaluate this expression and return the count for each store?
Also, what are performance implications?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许这可以帮助,从 apidocs
它用自定义方法填充 json 。
maybe this could help , from apidocs
it populate the json with custom method .