I have some rails models which don't need any persistence, however I'd like rails to think the model actually has attributes x, y, z so when calling methods like to_json in the controller I get them included for free.
For example,
class ModelWithoutTableColumns << ActiveRecord::Base
def x
return "Custom stuff here"
end
There is no column x in the database for Table "ModelWithoutTable" (sorry for the slightly confusing name!)
Anyone have an idea how to tackle this one?
Sounds like you want ActiveModel. Check out http://yehudakatz.com/2010/01/10/activemodel-make-any-ruby-object-feel-like-activerecord/ for a great walkthrough by Yehuda Katz. Specifically the section called "Serialization" for your to_json
requirements.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With