2011-06-21 117 views
1

我遇到了一些问题!我有通过has_and_belongs_to_many连接的Place模型和Category模型。我希望能够过滤出属于给定数组中至少一个类别的所有地方(int数组的ids)。到目前为止,我只能够通过一个单一的类别用下面的代码进行过滤:Rails按has_and_belongs_to_many属性过滤

@places = Place.find(:all, :include => :categories, :conditions => { "categories_places.category_id" => id}) 

所以基本上,而不是ID的我想要的ID。我希望这里有一些能够帮助的专家!我对此很陌生。

回答

3

这应该很好地诀窍。

some_array_of_ids = [1, 2, 3] 
@places = Place.find(:all, :include => :categories, :conditions => ['categories.id IN (?)', some_array_of_ids) 
+0

谢谢你和@ bor1s为快速反应。我不能尝试解决方案,直到明天,但我毫不怀疑它应该工作:)不幸的是,我不能upvote,因为太低代表:( – Cesar

0

尝试:

@places = Place.find(:all, :include => :categories, :conditions => ["categories_places.category_id IN ?", int]) 

其中int - 你的IDS

0

或数组,如果你想在一个范围内的链接

some_array_of_ids = [1,2,3] 
@places = Place.includes(:categories).where('categories.id IN (?)', some_array_of_ids)