Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在Rails应用程序中使用Desive gem,其中;“用户”;分为三种型号吗?_Ruby On Rails_Email_Devise_Associations - Fatal编程技术网

Ruby on rails 如何在Rails应用程序中使用Desive gem,其中;“用户”;分为三种型号吗?

Ruby on rails 如何在Rails应用程序中使用Desive gem,其中;“用户”;分为三种型号吗?,ruby-on-rails,email,devise,associations,Ruby On Rails,Email,Devise,Associations,假设我在Rails 4应用程序中有以下内容: class Person < ActiveRecord::Base has_many :email_addresses, as: :emailable has_one :user_account end class EmailAddress < ActiveRecord::Base belongs_to :emailable, polymorphic: true # There is an :address column

假设我在Rails 4应用程序中有以下内容:

class Person < ActiveRecord::Base
  has_many :email_addresses, as: :emailable
  has_one :user_account
end

class EmailAddress < ActiveRecord::Base
  belongs_to :emailable, polymorphic: true
  # There is an :address column
end

class UserAccount < ActiveRecord::Base
  belongs_to :person
end
class-Person
一个人可以有多个电子邮件地址。一个人也可以拥有一个用户帐户。(我将其移动到自己的模型中,因为并非所有人都是用户。)登录时,任何人的电子邮件地址都可以用作“用户名”

鉴于此,我想使用Desive gem。我看您可以指定应用身份验证的模型
User
被广泛使用,但我会使用
UserAccount
。然而,Deviate希望电子邮件(用户名)字段在这个模型中


当一个人注册一个用户帐户时,实际上会创建三个相关记录(
person
EmailAddress
,和
UserAccount
)。我不知道如何让Desive使用此设置。有什么想法吗?

一个选项是,将电子邮件方法从您的
用户帐户
委托给您的电子邮件模型,并覆盖finder
def self.find_first_by_auth_conditions(warden_conditions)
,由Desive登录过程使用。我发现了一个非常好的方法,深入地描述了这一点,另一个方法也一样。还有关于如何用多封电子邮件确认Desive帐户

由于您的设置有点复杂,您也可以使用EmailAddress作为主要设计模型,并将密码方法委托给
用户帐户


如果您必须使用confirmable而不仅仅是用户帐户来确认每个电子邮件地址,那么这可能非常有用。此设置将阻止您覆盖该查找程序,但您可能会遇到以前从未尝试过的委托密码的其他问题。

如果您使用的是ActiveRecord

首先,添加

attr_accessor :email
用户帐户
(我认为这是处理设计表单最简单的方法)

接下来,您需要修改designe登录过程。不过,在您的用户帐户中,覆盖Desive方法,例如

  def self.find_for_database_authentication(warden_conditions)
    conditions = warden_conditions.dup
    if email = conditions.delete(:email)
      where(conditions.to_h).includes(:email_addresses).where(email_addresses: {email: email}).first
    else
      where(conditions.to_h).first
    end
  end
您可能还需要定义以下内容以获得上述代码

class UserAccount < ActiveRecord::Base
  belongs_to :person
  has_many :email_addresses, through: :person
class用户帐户
这应该行得通,我已经用ActiveRecord测试过了,但是如果您使用的是mongoid,那么解决方案可能会有所不同

请注意,我已经修改了Desive文档中的代码以获得解决方案