开发者

Problem with getting a name string from a query

开发者 https://www.devze.com 2023-01-12 12:02 出处:网络
I\'m pretty new to rails and mongoid and I have a problem with extracting a string out of a query. I have a class Filteroption

I'm pretty new to rails and mongoid and I have a problem with extracting a string out of a query.

I have a class Filteroption

class Fieldoption

include Mongoid::Document

field :name, :type => String  
field :option_id, :type => Integer

end

and with this entries

 
+--------------------------+------------------------------------+-----------+  
| _id                      | name                               | option_id |  
+--------------------------+------------------------------------+-----------+  
| 4c6de6a11d41c86698000002 | Request URI                        | 1         |
| 4c6de6a11d41c86698000003 | Hostname                           | 4         |

When 开发者_StackOverflow社区I query for the name with the option_id 4

foobar = Fieldoption.only(:name).where(:option_id => '4')

foobar.name should be "Hostname". Instead I get

foobar.name

=> "Fieldoption"

What am I doing wrong ?

Thanks for your help.


The name method is already defined in Ruby to get the class name. You should rename your field to be able to get its content, or try foobar.attributes["name"].

0

精彩评论

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

关注公众号