设计:使用单表继承实例current_user

bri*_*lah 8 ruby-on-rails single-table-inheritance devise ruby-on-rails-3

我正在使用rails 3.0.9并设计用于身份验证.现在我正在尝试使用单表继承,因为我需要使用多态,所以我有两个类:UserType1和UserType2,它继承自User类.我根据用户的类型正确地需要Devise实例current_user.

例如,

class User < ActiveRecord::Base
 #devise and other user logic 
end

class UserType1 < User
  def get_some_attribute
     return "Hello, my type is UserType1"
  end
end

class UserType2 < User
  def get_some_attribute
   return "Hello, my type is UserType2"
  end
end

In controller 

class MyController < ApplicationController
  def action
    @message = current_user.get_some_attribute #depending the type using polymorphism
    render :my_view
  end
end
Run Code Online (Sandbox Code Playgroud)

rbi*_*ock 5

这正是您所需要的:http://blog.jeffsaracco.com/ruby-on-rails-polymorphic-user-model-with-devise-authentication

您需要重写应用程序控制器中的登录路径方法,希望它有所帮助。