1

ホームページの1つのログインフォームから、顧客、企業、ベンダーの3つのユーザータイプにログインする必要があるシステムがあります。

http://github.com/binarylogic/authlogic_exampleにあるAuthLogicのサンプルアプリに従って機能する1つのユーザーテーブルを作成しました。現在「顧客」、「会社」、または「ベンダー」のいずれかを含む「ユーザータイプ」というフィールドを追加しました。

注:各ユーザータイプには多くの異なるフィールドが含まれているため、単一テーブル継承が最善の方法であるかどうかはわかりません(この結論が無効な場合は修正を歓迎します)。

これは、3つのタイプのそれぞれがユーザーレコードで「タグ付け」されているポリモーフィックな関連付けですか?UserテーブルとユーザータイプCustomer、Company、Vendorの間に適切な関係を持たせるために、モデルはどのように見える必要がありますか?

どうもありがとう!

- - - アップデート - - -

現在の設定:

#User model
class User < ActiveRecord::Base
  acts_as_authentic
  belongs_to :authenticable, :polymorphic => true
end

#Customer model (Company and Vendor models are similar)
class Customer < ActiveRecord::Base
  has_many :users, :as => :authenticable
  acts_as_authentic
end

これはそれらを設定するための有効な方法ですか?

4

1 に答える 1

0

これはあなたがやろうとしているように聞こえます:

あなたはusersそれを3つのうちの1つにすることができますgroups。それが正しくない場合は、少し説明すると役立ちます。

AuthLogicは特別なフィールドのみを考慮しているため、ユーザーモデルで他のフィールドを作成して使用することは大したことではありません。

それぞれuserが次のように構成されている可能性があります。

#The Authlogic Fields
t.string :username,          :null => false
t.string :crypted_password,  :null => false
t.string :password_salt,     :null => false
t.string :persistence_token, :null => false

#Your field to identify user type
t.integer :user_type

#If your user is a customer, you would populate these fields
t.integer :customer_name
...

#If your user is a company, you would populate these fields
t.integer :company_name
...

#If your user is a vendor, you would populate these fields
t.integer :vendor_name
...

「単一テーブル継承」の意味はわかりませんが、ユーザーテーブルとは別のテーブルにaに関する情報を保持したい場合はvendor(パフォーマンスについて本当に心配しない限り、理由はありません)、リンクするだけです。このようなモデル:

class User < ActiveRecord::Base
  has_many :customers, :companies, :vendors
end

class Customer < ActiveRecord::Base
  belongs_to :user
end

class Company < ActiveRecord::Base
  belongs_to :user
end

class Vendor < ActiveRecord::Base
  belongs_to :user
end

この場合、ユーザーは3つのユーザータイプのうちの2つに関連付けられていないため、has_many関連付けが0の場合に適切に機能する関連付けを使用するようにプッシュされます。倍増しないように、コードでいくつかのチェックを行う必要があります。

于 2010-05-26T04:48:09.677 に答える