私は、(ユーザーの種類に応じて)分離不可能な多相関連を持つユーザークラスを持っています。新しいユーザーが作成されるたびに、対応するユーザータイプモデルも作成する必要があります。ユーザーは、通常の登録だけでなく、omniauth(facebook、google)を使用して作成することもできます。
質問:各アクションでユーザーとユーザーの種類を作成するか、ユーザーモデルでカスタム作成関数を追加し、ユーザーの種類といくつかのパラメータを渡してユーザーとユーザーの両方の型を作成する必要がありますか?ここでRails:モデルやコントローラにユーザークラス(多態的なassocを含む)を作成する必要がありますか?
は私の登録コントローラからアクションを作成することです:
def create
if params[:type] == 'employee'
@user_type = Employee.new sign_up_params[:user_type_attributes]
elsif params[:type] == "customer"
@user_type = Customer.new sign_up_params[:user_type_attributes]
elsif params[:type] == "owner"
@user_type = Owner.new sign_up_params[:user_type_attributes]
else
flash[:notice]="Invalid parameter"
redirect_to :controller=>'welcome', :action=>'register_choice'
return
end
build_resource(sign_up_params)
if resource.valid? && @user_type.valid?
ActiveRecord::Base.transaction do
super do
@user_type.save
resource.user_type = @user_type
if [email protected]_type.persisted? || !resource.persisted?
clean_up_passwords resource
set_minimum_password_length
flash[:alert]="Error"
raise ActiveRecord::Rollback
end
end
end
else
#TODO error messages
clean_up_passwords resource
set_minimum_password_length
flash[:alert]="Error"
respond_with resource
end
end
あなたはdeviseを使用しましたか? istは登録コントローラを作成しますか? – 7urkm3n
はい、それは上書きされたコントローラであり、動作します。しかし、私はomniauthコールバックでユーザーを作成したいので、createFrom(type、user_params、user_type_params)のようないくつかのモデルアクションでそれをコピーするかカプセル化する必要があるのだろうかと思います。 – FunkyFrankie