Mongoid virtual attributes in to_json


I'm trying to get some virtual (non-persisted) attributes to show up in the JSON representation of some Mongoid models, but can't seem to get it to work:

class MyModel include Mongoid::Document def virtual_attribute @my_attribute || false end def virtual_attribute=(value) @my_attribute=value end end class MyController def myaction false_values=MyModel.where( whatever ) true_values=MyModel.where( something_else ).map{ |model| model.virtual_attribute=true } @val['my_models']=false_values+true_values render json: @val.to_json( :include => {:my_models => {:methods => %w(virtual_attribute)}} ) end end

virtual_attribute doesn't appear in the json. What am I doing wrong?

<em>Edit</em> - ok, so I guess my actual problem is that I can't figure out how to invoke the virtual_attribute method on each of an array of objects that is nested in the root object.


to_json passes the options directly to the array and the objects. :include is only a Mongoid thing:

render json: @val.to_json(methods: :virtual_attribute)


  • How to use Ruby's metaprogramming to reduce method count
  • Pass in self when I initiate the class
  • Accessing one model from another
  • Executing a mixin method at the end of a class definition
  • Weird relationship behavior on Rails 3 and update_attribute
  • Milliseconds with Rails and Mysql
  • No error while instantiating abstract class, even though abstract method is not implemented
  • django admin - how to display thumbnail instead of path to file
  • Flask-Restful error: “as_view” method not inherited
  • Why is Rails redirecting my POST but not GET?
  • Pytest/Allure - How to generate testcase description?
  • redirect user after update in class based view in django
  • Paramiko SSHException Channel Closed
  • How to implement simple validation in Scala
  • Python find continuous interesctions of intervals
  • How to override __call__ in celery on main?
  • Curried UDF - Pyspark
  • Python function to read variable length blocks of data from file while open
  • MRO with multiple inheritance in python
  • How to move an element within a structure, possibly with zippers?
  • @tailrec why does this method not compile with 'contains a recursive call not in tail position&
  • Fully customized Python Help Usage
  • SPARQL date range
  • Ruby 1.8.6 Array#uniq not removing duplicate hashes
  • Faster Way To Simultaneously Iterate Over Rolling Window Of Two Or More Numpy Arrays?
  • Django foreign key drop down
  • Grails calculated field in SQL
  • Chart.js Multiple dataset
  • How do I get HTML corresponding to current DOM tree?
  • JQuery Internet Explorer and ajaxstop
  • JSON response opens as a file, but I can't access it with JavaScript
  • How to add date and time under each post in guestbook in google app engine
  • JSON with duplicate key names losing information when parsed
  • Change an a tag attribute in JavaScript based on screen width
  • Return words with double consecutive letters
  • How to stop GridView from loading again when I press back button?
  • Bitwise OR returns boolean when one of operands is nil
  • sending mail using smtp is too slow
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs