how i can render a json for an list of ActiveRecord in Rails?
for example, i got a list of Target in my controller
@targets = @user.targets.all
then i want to render @targets as json result for client ajax call. in Target class define, i have some column i don't want to render. i know when render single @ta开发者_开发技巧rget, we can use
:only
or
:except
but, how to deal with the list?
i tried
@targets.each do |target| xxxx end
to merge the json result, failed.
Override as_json
in your Target
class and specify which columns you don't want to be rendered.
More details here.
精彩评论