设计 – 从两个模型登录

我有两个用户模型,第一个来自远程数据库作为遗留和内部公司目的。 (员工登录)。 其次是我们的公共注册项目并登录,但我想要一个登录表单。 我已经搜索了很长时间,但有些解决方案对我来说很困惑。

第一个遗产看起来像(仅用于阅读和身份validation):

class CrmUser  [:login] alias_attribute :encrypted_password, :crypted_password alias_attribute :password_salt, :salt # Setup accessible (or protected) attributes for your model attr_accessible :login, :password, :password_confirmation, :remember_me, :role_id, :first_name, :last_name 

第二,对于公众和注册:

 class User  [:login] alias_attribute :login, :email # Setup accessible (or protected) attributes for your model attr_accessible :login, :password, :password_confirmation, :remember_me, :role_id, :first_name, :last_name 

现在我不知道怎么做用户控制器尝试从第一个模型进行身份validation,当用户不存在时,转到第二个模型并再次尝试。

使用:

  • Rails 3.1
  • 设计1.4.3

编辑:

在Devise的wiki中有关于多个模型的东西,但我有点困惑,没有更复杂的例子。

谢谢。

此致,Rado

你应该从find_for_authentication devise/models/authenticatable.rb monkeypatch find_for_authentication方法

 module Devise module Models module Authenticatable def find_for_authentication(conditions) #put your authentication logic here end end end end 

关于身份validation逻辑:在您的情况下使用两个模型进行身份validation,这实际上是个坏主意。 你想如何与两个用户模型建立关系? 这是很多不必要的代码。

解决问题的正确方法是在您的表之间进行一些同步。

  • 尝试使用基本用户模型对用户进行身份validation。

  • 如果用户凭据错误 – 尝试使用CrmUser模型对其进行身份validation。

  • 如果使用CrmUser进行身份validation是正确的,如果他已经存在,则将他添加到users表

  • 返回用户的模型对象。