Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8019319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:21:20+00:00 2026-06-04T21:21:20+00:00

In my app there is an association problem, which I’m unable to fix. My

  • 0

In my app there is an association problem, which I’m unable to fix.
My app is quite simple: There’s an Article model; each article has_many comments, and each of those comments has_many votes, in my case ‘upvotes’.
To explain the way I designed it, I did a comments scaffold, edited the comment models and routes to a nested resource, everything works fine. Now, I basically did the same process again for ‘upvotes’ and again edited model and routes to make this a nested resource within the comment nested resource. But this fails at the following point:

NoMethodError in Articles#show
Showing .../app/views/upvotes/_form.html.erb where line #1 raised:
undefined method `upvotes' for nil:NilClass

My _form.html.erb file looks like this:

<%= form_for([@comment, @comment.upvotes.build]) do |f| %>
<%= f.hidden_field "comment_id", :value => :comment_id %>
<%= image_submit_tag "buttons/upvote.png" %>
<% end %>

Why is ‘upvotes’ undefined in this case, whereas here:

<%= form_for([@article, @article.comments.build]) do |form| %>
rest of code

everything works totally fine? I copied the same mechanism but with @comment.upvotes it doesn’t work.

My upvotes_controller:

class UpvotesController < ApplicationController

  def new
    @upvote = Upvote.new

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

  def create
  @article = Article.find(params[:id])
  @comment = @article.comments.find(params[:id])
    @upvote = @comment.upvotes.build(params[:upvote])
    respond_to do |format|
      if @upvote.save
        format.html { redirect_to(@article, :notice => 'Voted successfully.') }
        format.xml  { render :xml => @article, :status => :created, :location => @article }
      else
        format.html { redirect_to(@article, :notice => 
        'Vote failed.')}
            format.xml  { render :xml => @upvote.errors, :status => :unprocessable_entity }
      end
    end
  end

end

I’m sorry for this much code.., my articles_controller: (extract)

def show
    @upvote = Upvote.new(params[:vote])
    @article = Article.find(params[:id])
    @comments = @article.comments.paginate(page: params[:page])


    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @article }
    end
  end

And my 3 models:

  class Article < ActiveRecord::Base
    attr_accessible :body, :title

    has_many :comments
  end

  class Comment < ActiveRecord::Base
    attr_accessible :content

    belongs_to :user
    belongs_to :article
    has_many :upvotes
  end

  class Upvote < ActiveRecord::Base
    attr_accessible :article_id, :comment_id, :user_id

    belongs_to :comment, counter_cache: true
  end

Upvote migration file:

  class CreateUpvotes < ActiveRecord::Migration
    def change
      create_table :upvotes do |t|
        t.integer :comment_id
        t.integer :user_id

        t.timestamps
      end
    end
  end

My routes:

  resources :articles do
      resources :comments, only: [:create, :destroy] do
        resources :upvotes, only: [:new, :create]
      end
    end

Sorry for that much code. If anyone might answer this, they would be so incredibly awesome!
Thank you in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T21:21:21+00:00Added an answer on June 4, 2026 at 9:21 pm

    Why is ‘upvotes’ undefined in this case, whereas here:

    This is because you’re calling upvotes on a nil object, the comment doesn’t exist yet.

    Best thing to do would be looking into nested attributes:

    http://guides.rubyonrails.org/2_3_release_notes.html#nested-attributes

    http://guides.rubyonrails.org/2_3_release_notes.html#nested-object-forms

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have this simple app working : # model class Project has_many :numbers
I am developing a Universal app. In this app there is a UITableView. Problem
A simple quiz: Probably many guys know this before, In my app there is
Assume that you are writing a simple app. The model is that a 'project'
In my app there is requirement that..I have 6 buttons in a nib, when
In my app there is a button, user just click it then the latest
In my app there is the view says googleBtn. If i press on it
I'm coding an app that works much like Apple's Weather.app: There's a UIPageControl at
When hosting WPF user controls within a WinForms MDI app there is a drawing
I am planning to start an iPad app... In my app there will be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.