我正在實現我的第一個HABTM關係,並且遇到了與我的查詢有關的問題。Rails 3 HABTM條件=數組查詢的適當形式
我正在尋找驗證我的方法並查看是否在AREL(或Rails的其他部分)代碼中發現了一個錯誤。
我有以下型號
class Item < ActiveRecord::Base
belongs_to :user
belongs_to :category
has_and_belongs_to_many :regions
end
class Region < ActiveRecord::Base
has_ancestry
has_and_belongs_to_many :items
end
我已在相關items_regions表:
class CreateItemsRegionsTable < ActiveRecord::Migration
def self.up
create_table :items_regions, :id => false do |t|
t.references :item, :null => false
t.references :region, :null => false
end
add_index(:items_regions, [:item_id, :region_id], :unique => true)
end
def self.down
drop_table :items_regions
end
end
我的目標是創建一個範圍/查詢如下:
查找所有項目一個區域(及其子區域)
祖先的寶石提供了一種方法來ret將區域的後裔分類爲數組。在這種情況下,
ruby-1.9.2-p180 :167 > a = Region.find(4)
=> #<Region id: 4, name: "All", created_at: "2011-04-12 01:14:00", updated_at: "2011-04-12 01:14:00", ancestry: nil, cached_slug: "all">
ruby-1.9.2-p180 :168 > region_list = a.subtree_ids
=> [1, 2, 3, 4]
如果只有一個陣列中的元件是,下面的作品
items = Item.joins(:regions).where(["region_id = ?", [1]])
生成的SQL是
"SELECT `items`.* FROM `items` INNER JOIN `items_regions` ON `items_regions`.`item_id` = `items`.`id` INNER JOIN `regions` ON `regions`.`id` = `items_regions`.`region_id` WHERE (region_id = 1)"
但是,如果在多個項目陣列和我嘗試使用IN
Item.joins(:regions).where(["region_id IN ?", [1,2,3,4]])
ActiveRecord::StatementInvalid: Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1,2,3,4)' at line 1: SELECT `items`.* FROM `items` INNER JOIN `items_regions` ON `items_regions`.`item_id` = `items`.`id` INNER JOIN `regions` ON `regions`.`id` = `items_regions`.`region_id` WHERE (region_id IN 1,2,3,4)
在SQL生成在末端
"SELECT `items`.* FROM `items` INNER JOIN `items_regions` ON `items_regions`.`item_id` = `items`.`id` INNER JOIN `regions` ON `regions`.`id` = `items_regions`.`region_id` WHERE (region_id IN 1,2,3,4)"
生成的代碼的最後部分的誤差應當是 (REGION_ID IN( 「1,2,3,4」))
如果我編輯SQL手動和運行它,我得到了我的期望。
於是,兩個問題:
- 是我的單值情況下的做法是否正確?
- SQL代是一個錯誤還是我配置錯誤的東西?
感謝 艾倫
感謝您的快速回答。我沒有提到我也試過這個...'Item.joins(:regions).where(:region_id => [1,2,3,4])'給出了以下錯誤'ActiveRecord :: StatementInvalid :Mysql ::錯誤:'where子句'中的未知列'items.region_id':SELECT'items'。* FROM'items' INNER JOIN'items_regions' ON'items_regions'.'item_id' ='items'.'id' INNER JOIN'regions' ON'regions'.'id' ='items_regions'.'region_id' WHERE' items'.'region_id' IN(1,2,3,4)'。它看起來像WHERE語句附加region_id項目而不是items_regions。 – 2011-04-12 23:22:24
一個值而不是多個給出相同的錯誤。 – 2011-04-12 23:25:05
我錯過了上面問題IN(?) - 問號周圍的問題。 – 2011-04-18 23:55:19