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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:34:04+00:00 2026-06-17T15:34:04+00:00

Following this RailsCast for nested model forms: class Survey < ActiveRecord::Base has_many :questions, :dependent

  • 0

Following this RailsCast for nested model forms:

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

and

class Question < ActiveRecord::Base
  belongs_to :survey
  has_many :answers, :dependent => :destroy
  accepts_nested_attributes_for :answers, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
  validates_presence_of :content
end

and

class Answer < ActiveRecord::Base
  belongs_to :question
  validates_presence_of :content
end

These are used to create a nested form with all 3 models.

Here’s the issue:
I can create a new survey, give the survey a title, leave the question content blank, add an answer and click submit.

The survey gets created. Because of the lambda, the blank question field is discarded as well as the not-blank answer field.

What can I do to make the validation catch when an answer is present but not a question, allowing the user to either delete the answer or provide a question?

  • 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-17T15:34:05+00:00Added an answer on June 17, 2026 at 3:34 pm

    One way is to remove the :reject_if when specifying the nested attribute.

    Or if you need more flexibility, you can update the lambda to do more things, for example: to reject if both question and answer are empty.

    Also look at the revised source code in github, which is helpful

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

Sidebar

Related Questions

I've been following RailsCast 197 to try this nested models/forms and have cracked my
I have a many-to-many model, following the example in this great railscast My model
I'm following this rails cast to create a nested form: http://railscasts.com/episodes/196-nested-model-form-part-1 I have a
I'm following this railscast to become familiar with ActionMailer in rails 3, but am
I'm using jquery_token_input after following Ryan Bates railscast #258 and this is the code
I have been following the railscasts 'Nested Model Form Parts 1 & 2' about
ok, I've been following: http://railscasts.com/episodes/196-nested-model-form-part-1 Here are the steps I've had to accomplish so
I'm following this railscast to get elasticsearch going in my rails app. When I
I'm currently following along with this railscast and for my specific situation am running
I'm using a nested form following rbates approach: http://railscasts.com/episodes/196-nested-model-form-revised I'm also using Twitter Bootstrap.

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.