Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 4: Unable to retrieve specific attributes of a model that `belongs_to` two other models?
    primarykey
    data
    text
    <p>So I have an <code>Active</code> model (think of it as a User) that uses Devise for authentication. I also have a <code>Rushee</code> model (you can think of them as products), and on each Rushee's profile page, I give Actives the option to leave a <code>Rusheepost</code> (think of them as product reviews).</p> <p>I'll post some of my code first, then will describe the problem.</p> <p>Here are my models:</p> <pre><code>class Rushee &lt; ActiveRecord::Base has_many :rusheeposts, dependent: :destroy class Active &lt; ActiveRecord::Base has_many :rusheeposts, dependent: :destroy class Rusheepost &lt; ActiveRecord::Base belongs_to :active belongs_to :rushee </code></pre> <p>routes.rb</p> <pre><code>devise_for :actives, :path_prefix =&gt; 'my' resources :actives, only: [:index, :show] resources :rushees, only: [:index, :show] do resources :rusheeposts, only: [:create, :destroy] end </code></pre> <p>RusheepostsController</p> <pre><code>before_action :authenticate_active! def create @rushee = Rushee.find(params[:rushee_id]) @rusheepost = @rushee.rusheeposts.build(rusheepost_params) @rusheepost.active = current_active if @rusheepost.save flash[:success] = "Comment created!" redirect_to @rushee else flash[:error] = "There was an error with your comment; please try again." redirect_to @rushee end end private def rusheepost_params params.require(:rusheepost).permit(:content) end </code></pre> <p>RusheesController (I only want signed in actives to be able to view rushees)</p> <pre><code>class RusheesController &lt; ApplicationController before_action :authenticate_active! def show @rushee = Rushee.find(params[:id]) @rusheeposts = @rushee.rusheeposts @rusheepost = @rushee.rusheeposts.build if active_signed_in? end def index @rushees = Rushee.all end end </code></pre> <p><code>show</code> view for Rushees</p> <pre><code>&lt;% provide(:title, @rushee.name) %&gt; &lt;div class="row"&gt; &lt;aside class="span4"&gt; &lt;section&gt; &lt;h1&gt; &lt;%= @rushee.name %&gt; &lt;%= @rushee.email %&gt; &lt;%= @rushee.grade %&gt; &lt;%= @rushee.major %&gt; &lt;/h1&gt; &lt;section&gt; &lt;%= render 'shared/rusheepost_form' %&gt; &lt;/section&gt; &lt;/section&gt; &lt;/aside&gt; &lt;!-- Displays the rusheeposts that are associated with the current rushee --&gt; &lt;div class="span8"&gt; &lt;% if @rushee.rusheeposts.any? %&gt; &lt;h3&gt;Comments (&lt;%= @rusheeposts.count %&gt;)&lt;/h3&gt; &lt;ol class="rusheeposts"&gt; &lt;%= render @rusheeposts %&gt; &lt;/ol&gt; &lt;% end %&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>_rusheepost_form.html.erb</p> <pre><code>&lt;%= form_for([@rushee, @rusheepost]) do |f| %&gt; &lt;%= render 'shared/error_messages', object: f.object %&gt; &lt;div class="field"&gt; &lt;%= f.text_area :content, placeholder: "Compose new comment..." %&gt; &lt;/div&gt; &lt;%= f.submit "Post", class: "btn btn-large btn-primary" %&gt; &lt;% end %&gt; </code></pre> <p>_rusheepost.html.erb</p> <pre><code>&lt;li&gt; &lt;span class="content"&gt;&lt;%= rusheepost.content %&gt;&lt;/span&gt; &lt;span class="timestamp"&gt; Posted &lt;%= time_ago_in_words(rusheepost.created_at) %&gt; ago by: &lt;%= rusheepost.active.name %&gt; &lt;/span&gt; &lt;/li&gt; </code></pre> <p><strong>The Problem</strong></p> <p>The <code>_rusheepost.html.erb</code> partial does not render. The <code>content</code> renders correctly every time. However, if I put <code>Posted &lt;%= rusheepost.created_at %&gt;</code>, it renders to: <code>Posted 2013-12-25 21:08:19 UTC</code>. However, <code>Posted &lt;%= time_ago_in_words(rusheepost.created_at) %&gt;</code> gets me a <code>undefined method '&gt;' for nil:NilClass</code> error.</p> <p>Furthermore, just putting <code>Posted by: &lt;%= rusheepost.active.name %&gt;</code> gets me the same nilclass error. Putting <code>Posted by: &lt;%= rusheepost.active %&gt;</code>, however, gets me: <code>Posted by: #&lt;Active:0x007ff3466108a0&gt;</code>.</p> <p>Strangely enough, everything works if I try to retrieve a rusheepost's rushee attribute. For instance, <code>Posted by: &lt;%= rusheepost.rushee %&gt;</code> yields <code>Posted by: #&lt;Rushee:0x007fa6681a56d8&gt;</code>. Similarly, <code>Posted by: &lt;%= rusheepost.rushee.name %&gt;</code> yields <code>Posted by: Theodora Willms III</code>, the name of the rushee who the post is associated with (and whose page I am on.) Obviously though, this isn't the functionality I want -- I want each post to display who <em>posted</em> it, not who the post is about.</p> <p>Does anyone have any ideas as to why this might be happening? Maybe I didn't initialize something correctly? I am failing to see why I cannot retrieve attributes of a rusheepost's active, but I can retrieve attributes of a rusheepost's rushee. I also must note that before I added the form to create new rusheeposts, I had the resources in my <code>routes.rb</code> listed on top of each other (not nested), and <strong>everything</strong> rendered correctly, including this: <code>Posted &lt;%= time_ago_in_words(rusheepost.created_at) %&gt; by: &lt;%= rusheepost.active.name %&gt;</code>. After I nested the resources and changed my controllers to reflect the nesting, the problems I describe appeared.</p> <p>Thanks, and sorry about the (very) long post. Let me know if there's any more information/code I can post that would help.</p> <p><strong>EDIT</strong>: I also must note that if I reset the database with no rusheeposts, then log in to my site, post on a rushee's page, and then go into <code>rails console</code> and type <code>Rusheepost.first.active.name</code>, I <em>do</em> in fact get the name of the Active that I was signed in as, which makes this all the more puzzling...</p>
    singulars
    1. This table or related slice is empty.
    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.
    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