0

我对 rails 和 mongoid 还很陌生,我在从查询中提取字符串时遇到了问题。

我有一个类 Filteroption

类 Fieldoption
包括 Mongoid::Document

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

结尾

和这个条目

 
+--------------------------+---------- --------------+------------+  
| _id | 姓名 | option_id |  
+--------------------------+---------- --------------+------------+  
| 4c6de6a11d41c86698000002 | 请求 URI | 1 |
| 4c6de6a11d41c86698000003 | 主机名 | 4 |

当我使用 option_id 4 查询名称时

foob​​ar = Fieldoption.only(:name).where(:option_id => '4')

foob​​ar.name 应该是“主机名”。相反,我得到

foob​​ar.name
=> "字段选项"

我究竟做错了什么 ?

谢谢你的帮助。

4

1 回答 1

1

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"].

于 2010-08-23T08:04:27.713 回答