Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在Rails3.1中,当类别通过:has_many,:through关联时,如何找到具有指定类别的商店_Ruby On Rails_Ruby On Rails 3_Activerecord_Has Many Through - Fatal编程技术网

Ruby on rails 在Rails3.1中,当类别通过:has_many,:through关联时,如何找到具有指定类别的商店

Ruby on rails 在Rails3.1中,当类别通过:has_many,:through关联时,如何找到具有指定类别的商店,ruby-on-rails,ruby-on-rails-3,activerecord,has-many-through,Ruby On Rails,Ruby On Rails 3,Activerecord,Has Many Through,在我的商店模式中,我有以下内容: class Store < ActiveRecord::Base has_many :categorizations has_many :categories, :through => :categorizations scope :by_categories, lambda{|category_ids| where(*stores have the following* => category_ids) unless c

在我的商店模式中,我有以下内容:

class Store < ActiveRecord::Base
  has_many :categorizations
  has_many :categories, :through => :categorizations

  scope :by_categories, lambda{|category_ids|
    where(*stores have the following* => category_ids) unless category_ids.empty?
  }
类存储:分类
范围:按类别,lambda{124;类别ID|
其中(*门店具有以下*=>类别标识),除非类别标识为空?
}
我希望设置一个作用域,在这个作用域中,我可以发送多个或单个类别ID,并具有属于所有指定类别的作用域返回存储

感谢您的关注。

类存储class Store < ActiveRecord::Base
  has_many :categorizations
  has_many :categories, :through => :categorizations

  scope :by_categories, lambda { |category_ids|
    joins(:categorizations).where('categorizations.category_id' => category_ids) unless category_ids.empty?
  }
end

>> Store.by_categories([1,2])
=> SELECT "stores".* FROM "stores" INNER JOIN "categorizations" ON "categorizations"."store_id" = "stores"."id" WHERE "categorizations"."category_id" IN (1, 2)
有很多:分类 有很多:类别,:到=>:分类 范围:按类别,lambda{124;类别ID| 联接(:分类).where('categorizations.category\u id'=>category\u id),除非category\u id.empty? } 结束 >>按类别存储([1,2]) =>从“存储”中选择“存储”。*在“分类”中的“存储”内部连接“分类”。“存储id”=“存储”。“id”中的“分类”。(1,2)中的“分类id”