私はRuby on Rails 3を使用しており、ハッシュ(key
、value
ペア)をカプセル化されたRubyクラス(テーブルレスモデル)にマップしてkey
をvalue
を返すクラスメソッドとしてマップしようとしています。私はカプセル化されたRubyクラス(テーブルレスモデル)のメソッドにハッシュキーをマップする方法は?
class Users::Account #< ActiveRecord::Base
def initialize(attributes = {})
@id = attributes[:id]
@firstname = attributes[:firstname]
@lastname = attributes[:lastname]
end
end
def self.to_model(account)
JSON.parse(account)
end
を持っているモデルファイルで
私のハッシュは、私は(デバッグ)
---
id: 2
firstname: Name_test
lastname: Surname_test
01を返し
account = Users::Account.to_model(hash)
を作ることができます
hash = {\"id\":2,\"firstname\":\"Name_test\",\"lastname\":\"Surname_test\"}
です
それに動作しますが、私は
account.id
をすれば、私は<Hash:0x00000104cda410>
は、クラス自体(!)ハッシュではないので、私が思う
NoMethodError in Users/accountsController#new
undefined method `id' for #<Hash:0x00000104cda410>
このエラーが発生します。また、私はaccount = Users::Account.to_model(hash)
をやっていることが正しいアプローチではないと思います。
どうしたのですか?これらのハッシュキーをクラスメソッドに「マップ」するにはどうすればよいですか?