开发者

Rails - ActiveResource returning hash instead of an object

开发者 https://www.devze.com 2023-02-10 22:21 出处:网络
I\'ve got the following code u = Client.get(:show_by_u开发者_StackOverflow社区sername, :username => username.downcase)

I've got the following code

u = Client.get(:show_by_u开发者_StackOverflow社区sername, :username => username.downcase)

When a valid user is returned, they seem to be getting returned as a hash instead of an object that I can call methods on

e.g. I have to access values like

u['id']

instead of

u.id

How can I get it to return it as an object?

Thanks


As described in the docs for the ActiveResource 'get' method, it does not convert them into ActiveResource::Base instances. As it says, you need to use the find method instead:

u = Client.find(:all, :from => :show_by_username, :params => { :username => username.downcase })
0

精彩评论

暂无评论...
验证码 换一张
取 消