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...

ndreckshage
  • 853
  • 9
  • 20
  • 4
    `user_domain = (:email).split('@').last` <- `:email` should be `email` I assume. – Chris Salzberg Nov 05 '12 at 06:50
  • are you sure this callback raise the above error? check the backtrace and line number – HungryCoder Nov 05 '12 at 06:51
  • run the same statement without equating it to the user varialbe – Narmadha Nov 05 '12 at 07:01
  • Could you maybe provide us with a bit more of the error message? – deRailed Nov 05 '12 at 11:08
  • > User.create!(name: "test", email: "testtt@gmail.com", password: "foobar")(0.2ms) SAVEPOINT active_record_1 User Exists (0.3ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = 'testtt@gmail.com' LIMIT 1 Company Load (0.3ms) SELECT "companys".* FROM "companys" WHERE "company"."domain" = 'gmail.com' LIMIT 1 (0.1ms) ROLLBACK TO SAVEPOINT active_record_1 NameError: undefined local variable or method `user' for # – ndreckshage Nov 05 '12 at 13:15
  • changing (:email) to email did work, allowed user to be saved. but after save, that users company_id was still nil. But thats progress, Ill keep experimenting when i get home from work tonight. – ndreckshage Nov 05 '12 at 13:23
  • Try changing (:company) to self.company also. Why not do this as validation, validating that the company exists to allow signup? – omarvelous Nov 05 '12 at 22:23

1 Answers1

1

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.

ndreckshage
  • 853
  • 9
  • 20