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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:16:45+00:00 2026-06-12T17:16:45+00:00

I have an Account model that accepts nested attributes for a user model. An

  • 0

I have an Account model that accepts nested attributes for a user model. An Account has_many users. So a user cannot exist without an account. I wrote this validation:

# users.rb
validates :account_id, presence: true, numericality: { only_integer: true }

During sign up, a user fills the account form, and a nested user form. However, because the account does not yet exist, my tests are failing with this error:

#<ActiveModel::Errors: ... @base=#<Account id: nil, title: "ACME Corp", subdomain: "acme1", created_at: nil, updated_at: nil>, @messages={:"users.account_id"=>["can't be blank"]}>

I always want to ensure a valid account id exists for the user, unless the user is being created via a nested form in Accounts#new. In other words, when an Account is being created at the same time, in which case no account id exists yet to provide to the user.

Is there a way to do this?

It may be a moot point since all new users will be created via a current_account object, a la current_account.users.build. But I want to be sure.

  • 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-12T17:16:46+00:00Added an answer on June 12, 2026 at 5:16 pm

    In User model:

    validates :account, :presence => true
    

    In Account model:

    has_many :users, :inverse_of => :account
    

    This simply validates that the parent object is present – even if it hasn’t been saved yet.

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

Sidebar

Related Questions

I have a nested model structure that looks like this: resources :users, :path =>
I have a User model that has the following default_scope: default_scope where(account_id: Account.current_account.id) If
I have a User and Account models with has_one association and nested attributes. My
I have an Account model that has_one User model, and a User model that
I have an Account model and a User model: class Account < ActiveRecord::Base has_many
I have an Account model with the following : has_one :primary_user, :class_name => User,
I have a User model and an Account model. The user has many accounts
I have a deep hierarchical model that looks like this: Accounts -> Venues ->Events
I've got a model that looks something like this: One Account has many Branches,
I have two model as follows class User < ActiveRecord::Base validates_associated :account end class

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.