views:

33

answers:

1

Hello all,

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

+1  A: 

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

floatless
> foobar.attributes["name"] doesn't work.Weired thing is that foobar.name works inside a scaffolded view for Fieldoption