Berek Bryan
Berek Bryan

Reputation: 14725

Can't display field of referenced mongo document in rails3

I am working on a To-Do application to get some practice with Rails3 and MongoDB.

I have an Item which was a tag and a tag will have many items associated with it.

My Issue: The show.html.erb does not want to display the tag name.

# models/item.rb
class Item
 include Mongoid::Document
 field :name
 field :completed, :type => Boolean
 referenced_in :tag  
end

# models/tag.rb
class Tag
 include Mongoid::Document
 field :name
 key :name
 references_many :items
end

# items_controller.rb
 def show
    @item = Item.find(params[:id])

    respond_to do |format|
         format.html # show.html.erb
         format.xml  { render :xml => @item }
    end
 end

<!-- items/show.html.erb -->
<p>Name: <%= @item.name %></p>
<p>Completed: <%= @item.completed %></p>
<p>Tag: <%= @item.tag.name %></p>

The third or tag line of the show file is what is giving my problems. The error is below:

Document not found for class Tag with id(s) 4cd75765f5c4932f19000002.

if I replace the line in question with the line below then the correct tag id is displayed (however I want the tag name obviously)

<p>Tag: <%= @item.tag_id %></p>

Upvotes: 1

Views: 616

Answers (1)

Jason Prins
Jason Prins

Reputation: 136

The Tag class has two attributes named 'name'. Try removing key :name

Upvotes: 1

Related Questions