【发布时间】:2021-11-11 06:49:15
【问题描述】:
我有 3 个模型用户项目错误。我想通过创建多对多关系。我在模型中创建关系我不知道它是否正确,用户有用户类型列,它是枚举类型用户类型包含开发人员、经理、QA
user.user_type.manager belong to many project it has one to many relation
user.user_type.developer has many project and many project belong to developer. it has many to many realtion
project has many bugs and bugs belong to project
developer has many bugs and many bugs belong to developer
错误模型
class Bug < ApplicationRecord
belongs_to :project
has_many :developers, -> { where user_type: :Developer }, class_name: 'User', through: :project, source: :bugs
end
项目模型
class Project < ApplicationRecord
has_many :bugs, dependent: :delete_all
has_many :developers, -> { where user_type: :Developer }, class_name: 'User', through: :users, source: :project
has_many :users //it belong to manager_id
end
用户模型
class User < ApplicationRecord
enum user_type: %i[Manager Developer QA]
has_many :projects
has_many :bugs
end
developer_bug 模型
class DevelopersBug < ApplicationRecord
has_many :bugs
has_many :users
end
project_developer 模型
class ProjectsDeveloper < ApplicationRecord
has_many :projects
has_many :users
end
【问题讨论】:
-
使用
snake_case作为您的枚举键。%i[manager developer qa]。ActiveRecord::Enum基于键生成方法,因此您将获得User.Developer,这是非常非常规的并且会导致错误。例如,如果您在没有显式使用self的情况下调用该方法,解析器将认为它是一个常量。 github.com/rubocop/…
标签: ruby-on-rails ruby database ruby-on-rails-3 database-design