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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:50:36+00:00 2026-06-16T22:50:36+00:00

I have a User model which has many roles . Roles contains a user_id

  • 0

I have a User model which has many roles. Roles contains a user_id field, which I want to validate_presence_of

The issue is: if I assign a role to user upon create, the validation fails because no user_id is set. Now, I do want to validate that a user_id exists, but I need to save the user before checking that.

The code currently looks like this:

@user = User.new(params[:user])
@user.roles << Role.new(:name => 'Peon') unless @user.has_roles?
if @user.save
  # ...

The only ways I can think of getting around the problem involves either disabling the validation, which I don’t want to do, or double-saving to the DB, which isn’t exactly efficient.

What’s the standard way for handling this issue?

  • 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-16T22:50:37+00:00Added an answer on June 16, 2026 at 10:50 pm

    I think you can get around the validation problem if you change your code to look like this:

    @user = User.new(params[:user])
    @user.roles.new(:name => 'Peon') unless @user.has_roles?
    if @user.save
      # ...
    

    If that doesn’t work, you could try changing you validation to this:

    class Role < ActiveRecord::Base
      belongs_to :user
      validates :user_id, :presence => true, :unless => Proc.new() {|r| r.user}
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a User model which has many projects and a Project model which
I have a User Model which has many fields including weight of the user.
I have a model Journey which has many Users (drivers). I want to be
So in my model a user has many roles through assignments and a role
Ive got the following problem. I have a model called user which has a
I have a simple User model which is associated to many Town objects using
I have a model in rails, lets say User , which i want to
I have a model Client which has many :tours, association in it. I am
I setup roles for the User model, for now, I just have a role
I have a users_manager engine which has a User model class. In an other

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.