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

  • Home
  • SEARCH
  • 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 6115933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:08:56+00:00 2026-05-23T15:08:56+00:00

(NOTE: Source code here https://github.com/cthielen/dss-evote ) I’ve got a simple voting application. A survey

  • 0

(NOTE: Source code here https://github.com/cthielen/dss-evote)

I’ve got a simple voting application. A survey is the set of questions to vote on, a ballot is a per-user instance of their preferences, and the ballot has_many preferences, which again, are unique to each user. Here’s the modeling:

class Ballot < ActiveRecord::Base
  belongs_to :survey
  has_many :preferences
end

class Survey < ActiveRecord::Base
  has_many :questions
  has_many :eligibilities
  has_many :ballots

  accepts_nested_attributes_for :questions, :allow_destroy => true

  attr_accessible :title, :description, :status, :deadline, :questions_attributes

  def owner
    Person.find(owner_id)
  end
end

class Question < ActiveRecord::Base
  belongs_to :survey
  has_many :preferences
end

class Preference < ActiveRecord::Base
  belongs_to :ballot
  belongs_to :question
end

routes.rb only has this:
resources :surveys do
resources :ballots
end

/surveys/1 seems to work, even /surveys/1/ballots. /surveys/1/ballots/new is where I run into issues:

in ballots_controller.rb:

def new
  @survey = Survey.find(params[:survey_id])

  @ballot = @survey.ballots.build

  @survey.questions.count.times { @ballot.preferences.build }

  respond_to do |format|
    format.html # new.html.erb
  end
end

(corresponding view)

<%= form_for [@survey, @ballot] do |f| %>
  <%= f.fields_for @ballot.preferences do |preferences_fields| %>
    <% for question in @preferences_fields %>
      <p>
    <%= f.label question.question %>
    <%= radio_button(question.id, "preference", "Yes") %> Yes
    <%= radio_button(question.id, "preference", "No") %> No
    <%= radio_button(question.id, "preference", "Decline") %> Decline
  </p>
    <% end %>
  <% end %>

  <div class="actions">
    <%= f.submit "Vote" %>
  </div>
<% end %>

Results in the error:

NoMethodError in Ballots#new

Showing /Users/cthielen/Projects/Work/dss-evote/app/views/ballots/_form.html.erb where line #2 raised:

undefined method `model_name' for Array:Class
Extracted source (around line #2):

1: <%= form_for [@survey, @ballot] do |f| %>
2:   <% f.fields_for @ballot.preferences do |preferences_fields| %>
3:     <% for question in @preferences_fields %>
4:       <p>
5:      <%= f.label question.question %>

Now, it appears an array is being formed instead of proper instances of the class, but I’m at a loss for how to properly fix this.

EDIT: I should mention the reason I’m attempting to build @ballot.preferences are that the preferences represent a person’s answer, and the length of preferences may change from survey to survey. So if a survey has six questions, @ballot.survey.questions.length will be 6, and I need to create 6 blank @ballot.preferences, which will then be represented by form_for and hopefully saved properly using a RESTful Create.

Thanks in advance for any help you can offer!

  • 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-05-23T15:08:56+00:00Added an answer on May 23, 2026 at 3:08 pm

    Ok, this is due to this line:

    @ballot.preferences = @survey.questions.map{|question| question.preferences.build}
    

    Because the mapping creates an Array which could not be used by the form_helper (expecting a Model name generally provided by an ActiveRecord Relation).

    You should stick to something like:

    @survey.questions.count.times { @ballot.preferences.build }
    

    Sidenote:

    <% fields_for @ballot.preferences do |preferences_fields| %>
    

    should be:

    <%= f.fields_for :preferences do |preferences_field| %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering what this snippet exactly does? (found here: http://code.google.com/p/google-app-engine-samples/source/browse/trunk/django_example/django_bootstrap.py ) # Make
Unfortunately I cannot post all of the source code here. I can describe the
I followed these instructions here: http://w3schools.com/razor/razor_example.asp NOTE: I'm using Web Matrix The example said
Note : The code in this question is part of deSleeper if you want
Note The question below was asked in 2008 about some code from 2003. As
Take note of the code for this small WPF C# program made with Microsoft
I have only this in my mxml source code: <?xml version=1.0 encoding=utf-8?> <mx:Canvas xmlns:mx=http://www.adobe.com/2006/mxml
Note, this might perhaps be best suited on serverfault.com, but since it is about
I'm trying to trace through the Python source code where a certain function is
I downloaded the lasted stable ruby source code from the ruby website (1.9.2-p180) and

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.