Rails Active Record field, dynamic value (evaluation)
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 implic开发者_Python百科ations?
Thanks.
maybe this could help , from apidocs
@events.to_json(:include => {
:images => {
:only => [], :methods => [:public_url] }})
it populate the json with custom method .
精彩评论