可以在多个表中引用用户表中的枚举角色列吗?

时间:2019-04-12 21:52:57

标签: ruby-on-rails enums rails-migrations pundit

我有一个带有Role属性的User模型,该模型是使用enum定义的。

enum role: {'Instructor': 0, 'Student': 1, 'Other': 2}

现在,我还有另一个表讲师,其中包含来自User表的引用。

我有一个课程表,其中包含来自教师的参考。

我只希望教师创建课程,而不希望其他角色。

我正在使用Pundit进行授权。创建新课程时遇到问题。

def create
  ...
  authorize @course

  @course.instructor = Instructor.where(user: current_user).first

以上查询正在回滚,但未保存课程。

任何建议都会有更大的帮助。

app/policies/course_policy.rb

Rollback Error

app/policies/course_policy.rb

 class CoursePolicy < ApplicationPolicy
  attr_reader :user, :model

  def initialize (user, model)
    @user = user
    @course = model
  end

  def index?
    true
  end

  def show?
    true
  end

  def create?
    @user.Instructor?
  end

  def update?
    @user.Instructor_of? @course
  end

  class Scope
    attr_reader :user, :scope

    def initialize(user, scope)
      @user = user
      @scope = scope
    end

    def resolve
      scope.all
    end
  end
end

回滚错误输出:

ActiveRecord::AssociationTypeMismatch (Instructor(#70064238051700) expected, got #<ActiveRecord::Relation []> which is an instance of Instructor::ActiveRecord_Relation(#70064238083180)):

app/controllers/courses_controller.rb:31:in `create'
Started POST "/courses" for ::1 at 2019-04-13 06:02:33 +0700
Processing by CoursesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"++6oBiY4MeOHZKyMwAJ8VqF9ACayve5e+cmMg7FqG4dbHVCfDpI3uqVK7g75+auf8OABUTEnXlm9jshWu/50EQ==", "course"=>{"name"=>"Ruby on Rails", "description"=>"jk.jlliyf", "start_date(1i)"=>"2019", "start_date(2i)"=>"4", "start_date(3i)"=>"12", "end_date(1i)"=>"2019", "end_date(2i)"=>"7", "end_date(3i)"=>"12"}, "commit"=>"Create Course"}
  User Load (0.7ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 2], ["LIMIT", 1]]
  ↳ /home/sagar/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/activerecord-5.2.3/lib/active_record/log_subscriber.rb:98
  Instructor Load (1.8ms)  SELECT  "instructors".* FROM "instructors" WHERE "instructors"."user_id" = $1 ORDER BY "instructors"."id" ASC LIMIT $2  [["user_id", 2], ["LIMIT", 1]]
  ↳ app/controllers/courses_controller.rb:31
   (0.4ms)  BEGIN
  ↳ app/controllers/courses_controller.rb:34
   (0.4ms)  ROLLBACK
  ↳ app/controllers/courses_controller.rb:34
  Rendering courses/new.html.erb within layouts/application
  Rendered courses/_form.html.erb (44.0ms)
  Rendered courses/new.html.erb within layouts/application (46.7ms)
Completed 200 OK in 267ms (Views: 124.6ms | ActiveRecord: 32.4ms)

课程模式:

app/models/course.rb

    class Course < ApplicationRecord
        belongs_to :instructor
    end

1 个答案:

答案 0 :(得分:0)

基于注释线程,似乎不需要使用Instructor模型。

由于在您的政策中,您已经将create的操作限制为具有user角色的Instructor,并且您从未期望Instructor拥有超过{ {1}},那么我想我会做:

user_id

现在,我敢打赌def create ... authorize @course @course.instructor = current_user ... end Course,而不是instructor_id。但是,没关系。您只需要这样做:

user_id

现在您将能够:

class Course << ApplicationRecord 

  belongs_to :instructor, class_name: 'User' # or whatever your user class name is.

  ...

end

并返回作为课程讲师的@course.instructor

我还打赌User通常可以让您执行以下操作:

Instructor has_many :courses

因此,您需要执行的操作类似于:

@instructor.courses

现在您将能够执行以下操作:

class User << ApplicationRecord 

  has_many :instructed_courses, class_name: 'Course', foreign_key: :instructor_id

  ...

end

我正在执行“ instructed_courses”,因为假定具有@user.instructed_courses current_user.instructed_courses 角色的User会想做以下事情:

Student

要实现这一目标,我想我会创建:

@user.enrolled_courses

然后:

class StudentCourse < Application 

    belongs_to :enrolled_course, class_name: 'Course'
    belongs_to :student, class_name: 'User' 

end

现在您应该能够做到:

class User << ApplicationRecord 

  has_many :instructed_courses, class_name: 'Course', foreign_key: :instructor_id
  has_many :student_courses, foreign_key: :student_id
  has_many :enrolled_courses, through: :student_courses

  ...

end

您可能还想做类似的事情:

@user.enrolled_courses
current_user.enrolled_courses

这样您就可以做到:

class Course << ApplicationRecord 

  belongs_to :instructor, class_name: 'User' # or whatever your user class name is.
  has_many :student_courses, foreign_key: :enrolled_course_id
  has_many :students, through: :student_courses
  ...

end

Shazam!