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