【问题标题】:No such column for attribute in a join table连接表中没有这样的属性列
【发布时间】: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


    【解决方案1】:

    该错误是由 TaskVolunteer 中的 foreign_key 选项错误引起的。

     belongs_to :volunteer, class_name: "User", foreign_key: :volunteer_id
    

    foreign_key 这里指的是users 表上的列,而不是tasks_volunteers 上的列。您可以删除外键选项。

    class TaskVolunteer < ActiveRecord::Base
        # task_id, volunteer_id, selected (boolean)
        belongs_to :task
        belongs_to :volunteer, class_name: "User"
    end
    

    已添加

    我不得不说,通过稍微改变命名并使用枚举来表示状态,您可以显着降低代码和认知复杂性。

    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?
    

    【讨论】:

    • 不幸的是仍然得到错误:SQLite3::SQLException: no such column: users.selected
    • 可能是where('tasks_volunteers.selected' =&gt; true)?
    • 这似乎是让它起作用的原因:保留外键并将条件写为-&gt; {where('task_volunteers.selected' =&gt; true)}(使task_volunteers单数)。
    • 根据这段代码你会不会碰巧知道如何将参与者添加到任务中?当我尝试创建TaskVolunteer,将其selected 属性设置为true 并将其附加到任务实例时,当我使用task.volunteerstask.participants 时,我仍然得到一个空数组
    • user.tasks &lt;&lt; task task.task_volunteers.where(volunteer: user).last.update(selected: true)
    猜你喜欢
    • 2013-09-28
    • 1970-01-01
    • 1970-01-01
    • 2016-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-14
    相关资源
    最近更新 更多