2010-05-26 2 views
1

홈페이지, 로그인 양식 하나에서 고객, 회사 및 공급 업체의 세 가지 유형의 사용자를 로그인해야하는 시스템이 있습니다.레일즈 : 다형성 사용자 테이블 AuthLogic의 좋은 아이디어?

나는 AuthLogic의 예제 응용 프로그램에 따라 http://github.com/binarylogic/authlogic_example에 따라 작동하는 하나의 User 테이블을 만들었습니다. 현재 '고객', '회사'또는 '공급 업체'가 포함 된 '사용자 유형'이라는 필드를 추가했습니다.

참고 : 각 사용자 유형에는 여러 가지 필드가 있으므로 단일 테이블 상속이 최선의 방법인지 잘 모르겠습니다 (이 결론이 유효하지 않은 경우 수정을 환영합니다).

3 가지 유형 각각이 사용자 레코드로 '태그 지정'되어있는 다형성 연관입니까? 내 모델은 어떻게 보이므로 내 User 테이블과 Customer, Company, Vendor 사용자 유형간에 올바른 관계가 있습니까?

대단히 감사합니다.

------ UPDATE ------

현재 설정 :

#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 

이 그들을 설정하는 유효한 방법이 있나요?

세 가지 groups 중 하나 일 수 있습니다 users이 : 이것은 당신이하려고하는 같은 소리 것입니다

답변

0

. 그것이 옳지 않다면 약간의 설명이 도움이 될 것입니다.

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 
... 

난 당신이 "단일 테이블 상속"무슨 뜻인지 모르겠어요하지만 당신은 별도의 테이블에 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 

를이 경우, 사용자가 아무런 연관이없는 것이기 때문에 2 3 사용자의 유형, 당신은 내가 밀었습니다. n 연관을 사용하여 잘 작동하는 has_many 연결을 사용합니다. 코드에서 몇 가지 검사를 수행하여 두 배가되지 않도록해야합니다.

+0

안녕하세요 James, 코드를 사용해 주셔서 감사합니다. 귀하의 첫 번째 옵션을 피하기를 원하는 이유는 고객 테이블, 회사 테이블 및 회사 테이블에 20 개 이상의 필드가 있고 사용자 테이블이 ~ 60 개의 필드로 끝나기를 원하지 않기 때문에 ~ 40 개는 항상 빈. 한 명만 시도해보고 잘 작동하는지 확인하겠습니다. 고맙습니다! – sscirrus