电源轨 - 为滤波器创建 HBTM 范围



>我正在尝试创建一个带有 2 个参数的过滤器 - 一个使用无线电输入进行编辑,另一个使用一组复选框进行编辑。我有 2 个模型通过has_and_belongs_to_many关联连接。我想创建一个带有嵌套模型 id 的scope。例如,我有网址:

/catalog?category=1&feature=1,2

作用域必须返回嵌套实例,其中ids 1 和 2

如何创建这样的范围?

我的类别模型:

class Lamp < ActiveRecord::Base
belongs_to :category, inverse_of: :lamps
has_and_belongs_to_many :features
validates :name, :preview, :small_preview, presence: true
scope :visibles, -> { where(hided: false) }
scope :category_filter, -> (category_id) { where(category_id: category_id) if category_id.present? }

end

我的功能模型:

class Feature < ActiveRecord::Base
has_and_belongs_to_many :lamps
validates :name, presence: true
validates :name, uniqueness: true
mount_uploader :ico, FeatureIcoUploader
scope :visibles, -> { where(hided: false) }
scope :ordered, -> { order(prior: :asc, id: :desc) }
def display_name; name end
end

请尝试此操作,并确保您的中间表名称正确(:features_lamps或lamps_features(

Lamp.joins("join features_lamps on lamps.id = features_lamps.page_id").where(["features_lamps.features_id = ?", features_id])

最新更新