9

I have seen lots of similar questions here but nothing that quite fits my need.

I am a pretty experience rails developer but this new project is my first time using both Rails 3 and Devise (I'm normally on authlogic).

My app has two different models that I want to authenticate via devise.

One, User is just a standard users model Two, Business is similar to a user, (it has an email address column too) but it has additional info in the database (address, phone number, etc..)

I want to be able to log them both in via the same login form. Then obviously once they are logged in they will be presented with different info depending on what type of Model has logged in.

It may or may not be relevant that I was planning on using OmniAuth to allow Users (though probably not businesses) to sign up/on via facebook.

Thanks!

What's the easiest way to go about doing this?

goddamnyouryan
  • 6,854
  • 15
  • 56
  • 105
  • 2
    Surely there would have to be a very good reason why you wouldn't have one user model, with different roles and a has_one relationship with the additional information available only to those with business role. – mark Mar 25 '11 at 21:07
  • i definitely thought about that, but it seemed like it would be making things more complicated than they had to be. Especially in regards to the Business signup form. But that was my plan, provided there was no easy way to go about it in devise. – goddamnyouryan Mar 25 '11 at 21:18
  • With the business sign up you can easily nest the additional attributes into the registration form. Then define conditional validation depending on the type of sign up. This in my opinion would be far easier than separating the models. – mark Mar 25 '11 at 21:41

2 Answers2

5

I think the only way to handle this would be to have your own custom sign in form and controller that determined the type of user and then sign them in correctly. I would recommend an approach like what mark mentioned for simplicity (take a look at something like CanCan to manage roles).

Another potential problem with having multiple user models is that you will have multiple versions of all the devise helper methods. So for current_<resource> and <resource>_signed_in? you would have current_user, current_business_user, user_signed_in? and business_user_signed_in?. Then you would either have to implement your own versions of these methods or you would need to check both versions everywhere you used them.

Braden Becker
  • 2,489
  • 20
  • 15
  • @johnnyPando did it end up looking something like? http://stackoverflow.com/questions/11846695/how-to-build-associations-for-multiple-roles – Ryan Castillo Aug 07 '12 at 20:23
2

Can do this in application_controller?

current_user = current_resource_a || current_resource_b

cactis
  • 205
  • 5
  • 5