我正在实施我的第一个 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
我的目标是创建一个范围/查询如下:
查找区域(及其子区域)中的所有项目
祖先 gem 提供了一种将 Region 的后代类别作为数组检索的方法。在这种情况下,
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生成是错误还是我配置错误?
谢谢艾伦