如何在rails中使用4个表的连接查询

时间:2015-03-16 06:50:34

标签: mysql ruby-on-rails ruby-on-rails-3

我有以下表格4个模型

class ItemCode < ActiveRecord::Base
  belongs_to :item_point
end

class ItemPoint < ActiveRecord::Base
  belongs_to :item
  has_many :item_codes
end

class Item < ActiveRecord::Base
  belongs_to :prodcut_category
  has_many :item_points
end

class ProductCategory < ActiveRecord::Base  
  has_many :items
end

现在,我必须使用 product_category 找到 item_codes 详细信息,因为我使用了内连接。这是mysql查询

SELECT *
FROM `item_codes` utc
INNER JOIN item_points rtp ON rtp.id = utc.item_point_id
INNER JOIN items ri ON ri.id = rtp.item_id
INNER JOIN product_catagories rpc ON rpc.id = ri.product_catagory_id
WHERE rpc.id =1
LIMIT 0 , 30

现在我必须以Acitve记录格式编写相同的查询。

ItemCode.joins(:item_point).joins(:item).joins(:product_catagory).where("product_catagories.id = 1")

获得以下错误

ActiveRecord::ConfigurationError: Association named 'item_points' was not found; perhaps you misspelled it?

那么如何以Active记录格式编写给定查询。

修改

product_catagories

+----------------------+--------------+------+-----+---------+----------------+
| Field                | Type         | Null | Key | Default | Extra          |
+----------------------+--------------+------+-----+---------+----------------+
| id                   | int(11)      | NO   | PRI | NULL    | auto_increment |
| client_id            | int(11)      | YES  |     | NULL    |                |
| category_name        | varchar(255) | YES  |     | NULL    |                |
| category_description | varchar(255) | YES  |     | NULL    |                |
| created_at           | datetime     | NO   |     | NULL    |                |
| updated_at           | datetime     | NO   |     | NULL    |                |
| scheme_id            | int(11)      | YES  | MUL | NULL    |                |
+----------------------+--------------+------+-----+---------+----------------+

item_points

+----------------+--------------+------+-----+---------+----------------+
| Field          | Type         | Null | Key | Default | Extra          |
+----------------+--------------+------+-----+---------+----------------+
| id             | int(11)      | NO   | PRI | NULL    | auto_increment |
| item_id        | int(11)      | YES  | MUL | NULL    |                |
| created_at     | datetime     | NO   |     | NULL    |                |
| updated_at     | datetime     | NO   |     | NULL    |                |
+----------------+--------------+------+-----+---------+----------------+

+ ---------------------------- + -------------- + - ---- + ----- + --------- + ------------

----+
| Field                      | Type         | Null | Key | Default | Extra          |
+----------------------------+--------------+------+-----+---------+----------------+
| id                         | int(11)      | NO   | PRI | NULL    | auto_increment |
| created_at                 | datetime     | NO   |     | NULL    |                |
| updated_at                 | datetime     | NO   |     | NULL    |                |
| product_catagory_id        | int(11)      | YES  | MUL | NULL    |                |
+----------------------------+--------------+------+-----+---------+----------------+

item_codes

+----------------------+--------------+------+-----+---------+----------------+
| Field                | Type         | Null | Key | Default | Extra          |
+----------------------+--------------+------+-----+---------+----------------+
| id                   | int(11)      | NO   | PRI | NULL    | auto_increment |
| item_point_id        | int(11)      | YES  | MUL | NULL    |                |
+----------------------+--------------+------+-----+---------+----------------+

2 个答案:

答案 0 :(得分:3)

试试这个:

ItemCode.joins(item_point: {item: :product_category}).where(product_category: {id: 1})

让我简化加入表

  

如果您有直接关系

# item_code belongs_to item_point
ItemCode.joins(:item_point) 

# ItemPoint has_many item_codes & belongs_to item
ItemPoint.joins(:item_codes, :item) 
  

当你有间接/嵌套关系

# ItemCode belongs_to item_point, item_point belongs_to item
ItemCode.joins(item_point: :item) 

# ItemCode belongs_to item_point, item_point belongs_to item, item belongs_to product_category 
ItemCode.joins(item_point: {item: :product_category})

答案 1 :(得分:0)

试试这个

UniqueItemCode.joins(item_points: [{ items: :product_catagories }]).where(product_catagory: {id: 1})