【发布时间】:2018-06-29 03:32:25
【问题描述】:
我有研讨会表和类别表。
车间模型:
has_many :workshop_categories, dependent: :destroy
has_many :categories, through: :workshop_categories
类别型号:
has_many :workshop_categories, dependent: :destroy
has_many :workshops, through: :workshop_categories
我有一个 WorkshopCategory 模型:
class WorkshopCategory < ActiveRecord::Base
belongs_to :workshop
belongs_to :category
end
我想根据类别查询活动(活动是一个范围)研讨会。在车间模型中尝试过这个:
def self.browse(params)
workshops = Workshop.active.order( "created_at DESC" )
workshops = workshops.joins(:categories).where(:categories => { :id => params[:category]}) if params[:category].present?
workshops
end
但我收到此错误:
ActiveRecord::StatementInvalid (PG::AmbiguousColumn: ERROR: column reference "id" is ambiguous
这个查询的正确方法是什么?我在 Internet 上找到的所有内容都已过时。
【问题讨论】:
标签: mysql ruby-on-rails postgresql activerecord