我通过UserTask表有多对多的关联。我需要从task(task.creator)获取类型创建者的用户。它应该像task.user_tasks.where(user_type:0),但是有rails关联。有可能吗?
这是我的用户模型
class User < ApplicationRecord
has_many :user_tasks, foreign_key: 'user_email', primary_key: 'email'
has_many :tasks, through: :user_tasks
end
这是我的任务模型
class Task < ApplicationRecord
has_many :user_tasks, dependent: :destroy
has_many :users, through: :user_tasks
end
这是我的UserTask模型
class UserTask < ApplicationRecord
belongs_to :user, foreign_key: 'user_email', primary_key: 'email'
belongs_to :task
enum user_type: [:creator, :allowed]
end
答案 0 :(得分:2)
您可以使用
获取它User.joins(:user_tasks).where(user_tasks: { user_type: 0 })