Ruby on rails 联接表中的属性没有这样的列
我正在尝试创建一个应用程序,用户可以选择志愿者来完成他们的任务。Ruby on rails 联接表中的属性没有这样的列,ruby-on-rails,activerecord,attributes,associations,jointable,Ruby On Rails,Activerecord,Attributes,Associations,Jointable,我正在尝试创建一个应用程序,用户可以选择志愿者来完成他们的任务。志愿者被视为参与者的方式是通过任务志愿者联接表上的所选布尔属性。不幸的是,当我试图找到某个特定课程的参与者时,我得到了以下错误: task = Task.create task.participants SQLite3::SQLException: no such column: users.selected 型号 class User < ActiveRecord::Base has_many :owned_tasks
志愿者
被视为参与者
的方式是通过任务志愿者
联接表上的所选
布尔属性。不幸的是,当我试图找到某个特定课程的参与者时,我得到了以下错误:
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
class用户{where(selected:true)},通过::任务志愿者,来源::志愿者
属于:所有者,类别名称:“用户”
结束
该错误是由TaskAutonovate中有故障的外键
选项引起的
belongs_to :volunteer, class_name: "User", foreign_key: :volunteer_id
外键
这里指的是用户
表中的列,而不是任务
。您只需删除外键选项即可
class TaskVolunteer < ActiveRecord::Base
# task_id, volunteer_id, selected (boolean)
belongs_to :task
belongs_to :volunteer, class_name: "User"
end
不幸的是,仍然会出现错误:
SQLite3::SQLException:没有这样的列:users.selected
可能where('tasks\u志愿者。selected'=>true)
?下面是让它起作用的方法:将外键保持不变,并将条件写为->{where('task\u志愿者。selected'=>true)
(使志愿者的任务成为单数).基于此代码,您是否知道如何将参与者添加到任务中?当我尝试创建任务志愿者
,将其选定的
属性设置为真
,并将其附加到任务实例中时,当我使用任务时,仍然会得到一个空数组。志愿者
或任务。参与者
用户.任务
class User < ActiveRecord::Base
has_many :participations, foreign_key: :participant_id
has_many :owned_tasks, class_name: "Task", as: :owner
end
class Task < ActiveRecord::Base
belongs_to :owner, class_name: 'User'
has_many :participations
has_many :participants, through: :participations, source: :participant
# Dynamically generates relations such as 'selected_participants'
Participation.statuses.keys.each do |status|
has_many "#{status}_participants".to_sym,
-> { where(participations: { status: status.to_sym }) },
through: :participations,
source: :participant
end
end
class Participation < ActiveRecord::Base
belongs_to :task
belongs_to :participant, class_name: "User"
enum status: [:interested, :selected]
end
user.participations.selected
participation.selected?