Note that there are some explanatory texts on larger screens.

plurals
  1. POAccessing a referenced field using mongoid/mongodb/rails
    primarykey
    data
    text
    <p>If I have a model...</p> <pre><code>class Post include Mongoid::Document field :link field :title field :synopsis field :added_on, :type =&gt; Date validates_presence_of :link embeds_many :replies references_one :topic end </code></pre> <p>and</p> <pre><code>class Topic include Mongoid::Document field :category referenced_in :post end </code></pre> <p>What would I need to code in index.html.erb to access data in topic or to add a topic to post.</p> <p>I tried post.topic but I get an undefined method error.</p> <p>Thank you very much.</p> <p>Edit:</p> <p>Here is the index.html code</p> <pre><code>&lt;div id="post"&gt; &lt;% @posts.each do |post| %&gt; &lt;div class="title_container"&gt; &lt;%= link_to post.title, post.link %&gt; || &lt;%= link_to 'Edit', edit_post_path(post) %&gt; || &lt;%= post.topic %&gt; &lt;/div&gt; &lt;% end %&gt; &lt;br /&gt; &lt;h2&gt;Topics&lt;h2&gt; &lt;% for topic in @post.topics %&gt; &lt;h3&gt;&lt;%= topic.category %&gt;&lt;/h3&gt; &lt;% end %&gt; &lt;/div&gt; </code></pre> <p>here is the posts_controller</p> <pre><code>class PostsController &lt; ApplicationController # GET /posts # GET /posts.xml def index @posts = Post.all respond_to do |format| format.html # index.html.erb format.xml { render :xml =&gt; @posts } end end # GET /posts/1 # GET /posts/1.xml def show @post = Post.find(params[:id]) respond_to do |format| format.html # show.html.erb format.xml { render :xml =&gt; @post } end end # GET /posts/new # GET /posts/new.xml def new @post = Post.new respond_to do |format| format.html # new.html.erb format.xml { render :xml =&gt; @post } end end # GET /posts/1/edit def edit @post = Post.find(params[:id]) end # POST /posts # POST /posts.xml def create @post = Post.new(params[:post]) respond_to do |format| if @post.save format.html { redirect_to(@post, :notice =&gt; 'Post was successfully created.') } format.xml { render :xml =&gt; @post, :status =&gt; :created, :location =&gt; @post } else format.html { render :action =&gt; "new" } format.xml { render :xml =&gt; @post.errors, :status =&gt; :unprocessable_entity } end end end # PUT /posts/1 # PUT /posts/1.xml def update @post = Post.find(params[:id]) respond_to do |format| if @post.update_attributes(params[:post]) format.html { redirect_to(@post, :notice =&gt; 'Post was successfully updated.') } format.xml { head :ok } else format.html { render :action =&gt; "edit" } format.xml { render :xml =&gt; @post.errors, :status =&gt; :unprocessable_entity } end end end # DELETE /posts/1 # DELETE /posts/1.xml def destroy @post = Post.find(params[:id]) @post.destroy respond_to do |format| format.html { redirect_to(posts_url) } format.xml { head :ok } end end end </code></pre> <p>Edit:</p> <p>I am also adding the relevant _form.html.erb code.</p> <pre><code>&lt;div class="field"&gt; &lt;%= f.label :topic_id %&gt; &lt;%= f.collection_select :topic, Post.topic, :id, :category, :prompt =&gt; "Select a Topic" %&gt; &lt;/div&gt; </code></pre> <p>Edit:</p> <p>Updated to 2.0.0.rc.7 still can't get it.</p> <p>Tried the key method in the railscast video (http://railscasts.com/episodes/238-mongoid) just for fun. I get a "BSON::InvalidObjectId in PostsController#update" error.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload