我正在爲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