views:

43

answers:

1

Hi, needing some help to just break down the problem.

I have new users who will put in User.Email in a signup process. (I am using Ruby on Rails).

For any given email, I want to parse out the @domain portion.

I then want to find in a different model, Companies, the company_id with the matching column for @domain.

I want to return that instance, and then pass that value into the User.new

I kind of get what needs to happen at the level described, but need some guidance on the next level down.

+1  A: 

I'd do something like this

In user.rb

before_validation_on_create :assign_company

def assign_company
  unless self.email.blank?
    self.company = Company.find_by_domain(/@(.+)/.match(self.email))
  end
end

This will call assign_company before validation when a user is created. Here you check to see that the email has been set and then find the company by extracting the domain from the email.

I haven't tested this, so it might not work as is, but it should hopefully point you in the right direction.

jonnii
TMail (the Ruby mail class) looks like it can find the domain for you.from the (incorrect, but fixed here) RDoc - http://tmail.rubyforge.org/rdoc/classes/TMail/Address.html#M000322: email = TMail::Address.parse("[email protected]") email.domain #=> "lindsaar.net"
Omar Qureshi
thanks, this get me in th write direction...will post results :)
Angela