【发布时间】:2015-06-21 22:57:30
【问题描述】:
我正在尝试创建一个应用程序,用户可以在其中选择志愿者来完成他们的任务。将volunteers 视为participants 的方式是通过TaskVolunteer 连接表上的selected 布尔属性。不幸的是,当我尝试查找特定课程的参与者时,我收到以下错误:
task = Task.create
task.participants
SQLite3::SQLException: no such column: users.selected
模型
class User < ActiveRecord::Base
has_many :owned_tasks, class_name: "Task", foreign_key: :owner_id
has_many :task_volunteers, as: :volunteer
has_many :volunteered_tasks, through: :task_volunteers
end
class TaskVolunteer < ActiveRecord::Base
# task_id, volunteer_id, selected (boolean)
belongs_to :task
belongs_to :volunteer, class_name: "User", foreign_key: :volunteer_id
end
class Task < ActiveRecord::Base
# owner_id
has_many :task_volunteers
has_many :volunteers, through: :task_volunteers, source: :volunteer
has_many :participants, -> {where(selected: true)}, through: :task_volunteers, source: :volunteer
belongs_to :owner, class_name: "User"
end
【问题讨论】:
标签: ruby-on-rails activerecord attributes associations jointable