在没有相应的数据库列轨道模型属性 [英] rails model attributes without corresponding column in db

查看:97
本文介绍了在没有相应的数据库列轨道模型属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些导轨机型,不需要任何持久性,但是,我想轨认为该模型实际上有属性X,Y,Z因此调用像to_json在我让他们免费包含控制器的方法时。

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.

例如,

class ModelWithoutTableColumns << ActiveRecord::Base

def x
   return "Custom stuff here"
end

没有x列在数据库中的表ModelWithoutTable(遗憾的稍微有一些混乱的名字!)

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?

推荐答案

听起来像是你想要加载ActiveModel。检查出的http://yehudakatz.com/2010/01/10/activemodel-make-any-ruby-object-feel-like-activerecord/一个伟大的演练由耶胡达·卡茨。特别是所谓的序列化为您 to_json 要求的部分。

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.

这篇关于在没有相应的数据库列轨道模型属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆