我有两种不同类型的用户想要使用相同的RESTful路由。我在用户和配置文件之间存在多态关联,这两种类型的配置文件是PatientProfile
和DoctorProfile
。我想要做的是为两种用户类型提供一种干净的方式,以便能够使用相同的控制器操作。
class User
belongs_to :profile, polymorphic: true
class PatientProfile
has_one :user, as: :profile
class DoctorProfile
has_one :user, as: :profile
我有一个Review
模型。
# reviews_controller.rb
def index
# for patients this would be all reviews they wrote
# for doctors it would be all reviews about them
current_user.reviews
end
我小心翼翼地运行用户而不是个人资料,因为我觉得它会更干净。但是现在我认为current_user
方法应该真正返回配置文件而不仅仅是用户。然后应在相应的配置文件模型中进行所有关联。
我的问题:我是否正确地思考这个问题,或者我错过了什么?我查看了CanCanCan
和Pundit
,但两者似乎都是为了允许特定的操作,而我却试图根据请求它的人返回不同的信息。