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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:36:02+00:00 2026-05-29T12:36:02+00:00

I have a Rails 3.1 project with multi-level nested forms which are reused. For

  • 0

I have a Rails 3.1 project with multi-level nested forms which are reused.

For instance, a Teacher has a nested Being.

class Teacher < ActiveRecord::Base
    has_one :being
    accepts_nested_attributes_for :being
end

An Entrant also has a nested Being

class Entrant < ActiveRecord::Base
    has_one :being
    accepts_nested_attributes_for :being
end

I would like to be able to validate birthdays (in the being model), but only if the parent of being is Entrant.

In the being model:

attr_accessor :no_birthday
validates :birthday, :presence => {:message => 'Birthday is required to enter classes.'}, :unless => :no_birthday?

def no_birthday?
    unless self.no_birthday
        errors.add(:entrant, "Birthday is required.")
    end
end

In the form (teacher version only)

<%= hidden_field_tag 'no_birthday', "1" %>

And, just in case, in the teacher_controller:

def create
...
    @teacher = Teacher.new(params[:teacher])
    @teacher.being.no_birthday = 1
...
end

Somehow, I am very far from the mark. I can make the validation work for everybody, or nobody. I feel like there is a simple solution running parallel to me. Anybody know what that may be?

  • 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-29T12:36:04+00:00Added an answer on May 29, 2026 at 12:36 pm

    So, a few problems with the above approach:

    1) the validation line. while it seems like :unless should be a real thing, it doesn’t seem to be the case. reformat as an :if statement (in the being model):

    validates :birthday, :presence => {:message => 'Birthday is required to enter classes.'}, :if => :birthday_needed?
    

    I also changed the name of the method in the model to make better english sense as an if instead of unless.

    def birthday_needed?
        birthday_needed
    end
    #checks for trueness of accessor named birthday_needed
    

    2) I can solve the problem by setting an accessor from the required controllers. So, one parent’s controller will set the accessor to true, and another will not. No hidden form fields weirdness or any of the other random approaches I was looking for:

    parent #1’s create controller, as an example:

    def create
      ...
      @entrant.being.birthday_needed = true
    end
    

    the other parent will simply not have this accessor set at all.

    Of course, I will have to create an appropriate accessor for the being model or the whole thing will error out:

    attr_accessor :birthday_needed
    

    And now, it all works. Teachers don’t need birthdays and entrants do. Finally both parents can get along.

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

Sidebar

Related Questions

I have a Rails project which has a Postgres database for the actual application
I have a three-level multi-nested form in Rails. The setup is like this: Projects
I have created a instance variable in rails project, which gets its value from
I have a rails project deployed on heroku which has only a production environment.
I have a Rails project which I neglected to build tests for (for shame!)
I have joined a rails project as a contractor. The project has been going
I have a Rails 3 project in which I want to store the current
I have an existing Ruby on Rails project. How do I find out which
I'm joined a rails project which have been going for past two months. I
I currently have a Ruby on Rails project that has CarrierWave running on it.

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.