Mysql 如何对rails中的4个表使用联接查询
我有下面的表4模型Mysql 如何对rails中的4个表使用联接查询,mysql,ruby-on-rails,ruby-on-rails-3,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_p
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
现在我必须以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?
那么如何以活动记录格式编写给定的查询呢
编辑
产品类别
+----------------------+--------------+------+-----+---------+----------------+
| 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 | |
+----------------------+--------------+------+-----+---------+----------------+
+----------------+--------------+------+-----+---------+----------------+
| 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 |
| item_point_id | int(11) | YES | MUL | NULL | |
+----------------------+--------------+------+-----+---------+----------------+
项目积分
+----------------------+--------------+------+-----+---------+----------------+
| 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 | |
+----------------------+--------------+------+-----+---------+----------------+
+----------------+--------------+------+-----+---------+----------------+
| 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 |
| item_point_id | int(11) | YES | MUL | NULL | |
+----------------------+--------------+------+-----+---------+----------------+
项目
+----------------------+--------------+------+-----+---------+----------------+
| 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 | |
+----------------------+--------------+------+-----+---------+----------------+
+----------------+--------------+------+-----+---------+----------------+
| 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 |
| item_point_id | int(11) | YES | MUL | NULL | |
+----------------------+--------------+------+-----+---------+----------------+
+----------------------------+--------------+------+-----+---------+------------
项目代码
+----------------------+--------------+------+-----+---------+----------------+
| 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 | |
+----------------------+--------------+------+-----+---------+----------------+
+----------------+--------------+------+-----+---------+----------------+
| 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 |
| item_point_id | int(11) | YES | MUL | NULL | |
+----------------------+--------------+------+-----+---------+----------------+
试试这个:
ItemCode.joins(item_point: {item: :product_category}).where(product_category: {id: 1})
让我简化连接表的过程:当您有直接关系时: 当您有间接/嵌套关系时: 试试这个
UniqueItemCode.joins(item_points: [{ items: :product_catagories }]).where(product_catagory: {id: 1})
你能提供你的表格结构吗?@NavjotSingh我已经更新了我的问题。@I-am-simple-user,你试过我的解决方案了吗?