How to use join query for 4 tables in rails

2020-07-24 06:02发布

I have following tables 4 models

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

Now I have to find item_codes details using product_category for this I used inner join. Here is mysql query

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

Now I have to write the same query in the Acitve record format.

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

getting following error

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

So how can write given query in the Active record format.

EDIT

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    |                |
+----------------+--------------+------+-----+---------+----------------+

items

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

----+
| 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条回答
一夜七次
2楼-- · 2020-07-24 06:46

try this

UniqueItemCode.joins(item_points: [{ items: :product_catagories }]).where(product_catagory: {id: 1})
查看更多
霸刀☆藐视天下
3楼-- · 2020-07-24 06:48

Try this:

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

Let me simplify the Joining Tables:

When you have direct relation:

# item_code belongs_to item_point
ItemCode.joins(:item_point) 

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

When you have indirect / nested relation:

# 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})
查看更多
登录 后发表回答