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 6365897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:21:07+00:00 2026-05-25T00:21:07+00:00

Hi, I’m a Rails newbie and this is my first stackoverflow question. I’m currently

  • 0

Hi, I’m a Rails newbie and this is my first stackoverflow question.

I’m currently writing a poll application, the concept is very simple.
The User can create a new poll by giving it a title and some possible answers.

The answers can be added or removed by using javascript:void(0) links (see picture)

Everything works great so far (thanks to ryanb’s nested_form gem), but now I’m asking myself how can I count the number of the given answers, so I can limit it to say at most 5 answers per question?
(currently you can create an unlimited number of answers)

The answer creating part of the poll form looks like – THIS – in the browser.

Poll-Model (poll.rb):

class Poll < ActiveRecord::Base
  has_many :answers,  :dependent => :destroy
  accepts_nested_attributes_for :answers, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => true
end

Answer-Model (answer.rb):

class Answer < ActiveRecord::Base
  belongs_to :poll
  validates_presence_of :name
end

My current approach of counting the answers (polls_controller.rb):

def new
  @poll = Poll.new
  2.times { @poll.answers.build }
end


def create  
  ...

  if @poll.answers.count > 10
    flash[:alert] = 'Too many answers (not exceeding 10), please remove some'
    render :action => 'new'
    return
  end

  if @poll.save
    redirect_to poll_path(category_slug(@poll), @poll.id), :notice => 'Successfully created poll.'
  else
    render :action => 'new'
  end

  ...
end

Obviously my method won’t work, because the answers aren’t created yet, so my question is:

  • How and where (JavaScript, Polls-Controller, Poll-Model) should I count the number of the given answers, so I can limit the number

Thanks, I really appreciate every answer.

  • 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-25T00:21:08+00:00Added an answer on May 25, 2026 at 12:21 am

    Does :limit as part of nested_attributes work?

    e.g.

    accepts_nested_attributes_for :answers, :limit => 5, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => true
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want use html5's new tag to play a wav file (currently only supported

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.