我有三张桌子:
- 场地
- 空间(属于场地)
- 包含的空间(属于空间)
我在路线中收到Venue的id,并返回我知道包含空格的所有相关空格(空间记录中名为num_included_spaces__c的字段,用于维护其子项的计数)。现在我已经拥有该场地的所有相关父级空间,我需要为它们找到所有包含的空间。
包含的空间仍然是一个空间,恰好有一个父级位于同一个表中。我试图改变这个:
Venue = Rockdog
- 空间=楼上
- 空间=媒体室
- 空间=庭院
- 空间=楼下
- 空间=前庭院
- 空间=室内酒吧
进入这个:
Venue = Rockdog
- 空间=楼上
- 包含的空间=媒体室
- 包含的空间=庭院
- 空间=楼下
- 包含的空间=前庭院
- 包含的空间=室内酒吧
Included Spaces表包含belongs_to__c和space__c作为字段,其中belongs_to__c是父空间的id,space__c是子ID的id。所以我希望找到所有包含的空格,其中belongs_to_c匹配下面返回的任何@spaces的id
@sub_spaces = Space.where("venue__c = ? AND num_included_spaces__c = ?", params[:venue],0)
@spaces = Space.where("venue__c = ? AND num_included_spaces__c > ?", params[:venue],0)
如何为@included_spaces编写此Active Record查询?
答案 0 :(得分:0)
我的数据库架构。
mysql> describe venues;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
4 rows in set (0,00 sec)
mysql> describe spaces;;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| venue_id | int(11) | YES | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
5 rows in set (0,00 sec)
ERROR:
No query specified
mysql> describe included_spaces;;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| space_id | int(11) | YES | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
5 rows in set (0,00 sec)
ERROR:
No query specified
下面的函数将以某种方式为您提供所需的结果(在控制台中),但它不是一个好的解决方案 - 它比需要的查询数据库更多。然而,它很容易 - ))
def foo id
v = Venue.find(id)
puts v.name
v.spaces.each do |space|
puts space.name
space.included_spaces.each do |spis|
puts spis.name
end
end
end
您还可以尝试更复杂的查询,例如
mysql> SELECT spaces.name, included_spaces.name FROM `spaces` INNER JOIN `venues` ON `venues`.`id` = `spaces`.`venue_id` INNER JOIN `included_spaces` ON `included_spaces`.`space_id` = `spaces`.`id` WHERE `spaces`.`venue_id` = 1
-> ;
+------------+-----------+
| name | name |
+------------+-----------+
| Upstairs, | Front |
| Upstairs, | Patio, |
| Upstairs, | Indoor |
| Upstairs, | Bar |
| Downstairs | Media |
| Downstairs | Room, |
| Downstairs | Courtyard |
+------------+-----------+
7 rows in set (0,00 sec)
应该转换为活动记录
Space.joins(:venue)
.joins(:included_spaces)
.where(venue_id: 1)
.select('spaces.name, included_spaces.name')