Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rails model attributes without corresponding column in db

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?

like image 386
nc. Avatar asked Dec 30 '08 03:12

nc.


1 Answers

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.

like image 132
Adam Tanner Avatar answered Nov 14 '22 22:11

Adam Tanner