我正在为iPhone应用程序的后端构建Rails服务器。 Rails将JSON发送到前端,我发现自己正在做这样的事情。为模型实例对象创建属性的最佳方法
@user = User.find(1)
@user["status"] = "Some cool status"
render :json => @user.to_json
在我的RSpec的测试中,我得到
DEPRECATION WARNING: You're trying to create an attribute `status'. Writing arbitrary attributes on a model is deprecated. Please just use `attr_writer` etc.
我发现很难找到一个合适的替代时,它只是简单写一个键值,将被发送到iPhone的对象。
我的问题是什么是一些可行的替代方案,我试图做什么,除了弃用之外,我的代码特别“错误”。
是否有你不想使用'attr_accessor'的原因? – PinnyM 2013-04-23 20:47:47
有时我可能会将属性设置为唯一名称,或者我会在极少数情况下使用此技术。我不确定采用这种方法有多传统,因此我在问。 – jason328 2013-04-23 20:49:50
由于废弃状态,这不会被支持 - 请参阅http://stackoverflow.com/questions/10596073/deprecation-warning-for-creating-attribute-currency – PinnyM 2013-04-23 20:53:35