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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:53:27+00:00 2026-06-13T20:53:27+00:00

I’m trying to assign a user to their companys group based on their email

  • 0

I’m trying to assign a user to their companys group based on their email domain. I’m using devise + confirmation, so I avoided using regex (dont need to validate that its a valid email…), and am trying to do this in a simple way. So essentially, it would force the users company_id (which matches up with that table) to be assigned in sign up, and then not allow them to sign up if their company doesnt exist. So this will work for both test@company.com and test@recruiting.company.com

In User model

before_create :company_placement

…

def company_placement
  user_domain = (:email).split('@').last

  while user_domain.split('.').count > 2
    user_domain = user_domain.split('.', 2).last
  end

  if Company.find_by_domain(user_domain) != nil
    (:company_id) = Company.find_by_domain(user_domain).id
  else
    #error out
  end
end

When I do this in rails console step by step, it seems to work. But in console when i run,

> user = User.create!(name: "test", email: "test@example.com", password: "foobar")

I get undefined local variable or method ‘user’ for #<‘User….

Thanks for any help, still learning rails…

  • 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-13T20:53:29+00:00Added an answer on June 13, 2026 at 8:53 pm

    So I played with this some more and think i found a solution I like

    in user model

    before_validation :company_placement
    
    ...
    
    def company_placement
      user_domain = self.email.split('@').last
    
      while user_domain.split('.').count > 2
        user_domain = user_domain.split('.', 2).last
      end
    
      if Company.find_by_domain(user_domain) != nil
        self.company_id = Company.find_by_domain(user_domain).id
      end
    end
    

    Created devise registration controller — controllers/registrations _ controller.rb

    in new registrations controller

    class RegistrationsController < Devise::RegistrationsController
      before_filter :verify_company, only: :create
    
      private
    
        def verify_company
          build resource #necessary for devise
    
          user_domain = resource.email.split('@').last
    
          while user_domain.split('.').count > 2
            user_domain = user_domain.split('.', 2).last
          end
    
          unless Company.find_by_domain(user_domain) != nil
            flash[:error] = 'Sorry, your company does not exist yet'
            redirect_to root_path
          end
        end
    end
    

    routes.rb

    devise_for :users, :controllers => { :registrations => "registrations" }
    

    So im sure there is a more elegant solution, but this works for me. Handles the errors/flash in the controller, and then if the company exists, it the user gets auto assigned to the company through the model.

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

Sidebar

Related Questions

I have a small JavaScript validation script that validates inputs based on Regex. I
I'm trying to select an H1 element which is the second-child in its group
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:

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.