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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:39:33+00:00 2026-05-19T03:39:33+00:00

I have a nested Form A Crew can have many meetings, a Meeting can

  • 0

I have a nested Form

A Crew can have many meetings, a Meeting can belong to one Crew.
A Crew, and a Meeting can both have set a gender_id attribute.

I want to add an error to the Meeting object when Crew.meeting_id != Meeting.gender_id

I have written a simple validator

class MeetingGenderValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    if record.gender_id != record.crew.gender_id
      msg = :wrong_gender
      case record.crew.gender_id
        when 1 then
          msg = :crew_woman_only
        when 2 then
          msg = :crew_man_only
        record.errors.add(attribute, msg, options)
      end
    end
  end 
end

The problem is, When this validator is run, The record variable (our Meeting model), is not yet associated with the Crew model, so I get an error on calling Nil.gender_id

Here is the part of Meeting model:

class Meeting < ActiveRecord::Base
  belongs_to :crew 
  validates :gender_id, :meeting_gender => true
end

Here is the part of Crew model with the association:

class Crew < ActiveRecord::Base
  has_many :meetings
  accepts_nested_attributes_for :meetings
end

And my Crews#Create action snippet (controller):

class CrewsController < ApplicationController
  def create
    @crew = Crew.new(params[:crew]) # here are the meeting params too
    @crew.user = current_user # assigning the user from session / not important here
    if @crew.save
      ......
  end
end
  • 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-19T03:39:33+00:00Added an answer on May 19, 2026 at 3:39 am

    Have found a workaround, in the Crews controller:

    def create
      @crew = Crew.new(params[:crew])
      if @crew.meetings.to_a.size > 0
        @meeting = @crew.meetings.first
        @meeting.crew = @crew
        @crew.meetings = [] 
        @crew.meetings << @meeting
      end
      @crew.user = current_user
      ....
    end
    

    I now, it’s not pretty, but it works, it can be delegated to a model method, but I want show here a working solution. The question is still open…

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

Sidebar

Related Questions

I have created nested form and want to test it from terminal. How can
I have a nested form Parent, which accepts attribute for Child. In my controller#new,
I have a nested json. I want to post it as a form input
I have a nested form with 3 related models. I want all the fields
I have a polymorphic model and want to have a nested form use this
I want to scrape some html pages that have nested form elements with lxml.
I have a nested form and once I save, I want to be able
I have a simple nested form and I am setting a nested attribute with
I have a nested form that has a select dropdown menu. How can I
I have a nested form ,and i want to include city_id in the stores

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.