0

Rails4 ans Mongoid で単一テーブル継承のデバイスを使用しています

Class User
  #devise class
  validates :email, :uniqueness => { :scope => :_type }
end

class Patient < User
end

class Doctor < User
end

class Hospital < User
end

医師としてアカウントを作成する_type: "Doctor"場合、患者または病院としてアカウントを作成する場合、タイプは次のようになります。次に、いずれかのアカウント タイプの電子メールが既に存在する場合は、まずデータベースにチェックインし、その電子メールを使用して以前の資格情報でアカウントを作成します。

そのため、他のアカウントの種類を作成するために登録するときに、すべてのデータを提供したくありません。

このシナリオを達成する方法!!! 私を助けてください......

4

1 に答える 1

0

私があなただったら、継承は使いません。私はこのようなことをします:

Class User
  field :name
  field :email
end

Class Patient
  has_one :user

  attr_accesor :email
  field: disease

  before_create do
    existing_user = User.where( email: email ).first

    if uxisting_user
      user = uxisting_user
    else
      # you need create first an user
    end
  end

  def name
    user.name
  end
end

ユーザーが既に存在する場合、検索に必要なのはメールフィールドだけで、次のすべての情報を取得できます。

User.create( name: 'User1', email: 'user1@email.com' )
Patient.create( email: 'user1@email.com', disease: 'headache' )
Patient.first.name
=> "User1"

また、ユーザー情報を変更すると、Patient でも変更され、他のクラスでも同様です。

于 2014-01-13T13:39:30.607 に答える