2017-04-26 93 views
0

我有三個表:
- 地點
- 空間(屬於館)
- 包括空間(歸屬於空間)
從兩個表返回的數據集返回過濾記錄

我收到路線中Venue的id並返回我知道的所有相關空間都包含Spaces(空間記錄中的一個名爲num_included_spaces__c的字段,用於維護其子項的計數)。現在,我擁有該場所的所有相關父空間,我需要爲它們查找所有包含空格。

一個包含的空間仍然是一個空間,它恰好有一個駐留在同一個表中的父項。我試圖把這個:

地點= Rockdog
- 空間=樓上
- 空間=媒體室
- 空間=庭院
- 空間=樓下
- 空間=前天井
- 空間=室內酒吧

進入這個:

地點= Rockdog
- 空間= Upsta IRS
- 包括空間=媒體室
- 包括空間=庭院
- 空間=樓下
- 包括空間=前天井
- 包括空間=室內酒吧

的包括的空格表具有belongs_to__c和space__c作爲字段,其中belongs_to__c是父空間的ID,space__c是子級的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包括的空間?

回答

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 

以下功能會以某種方式給你你需要(在控制檯ofcourse),但是這不是一個很好的解決方案的結果 - 它查詢數據庫所需以上。然而,它是最簡單的 - ))

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') 
+0

我想我可能會問我的問題不對,但您的包含空間沒有belongs_to和空格字段。它看起來好像你假設包含空間不是空間,它們是。包含的空間更多地是空間具有父母的參考。額外的皺紋是,空間表和包含空間都沒有我可以使用的主鍵,所以我將不得不加入外鍵。 – csakon

+0

不,我聲明瞭belongs_to和has_many的層次結構:''場地'有很多'空間','空間'也有很多'包含空間','包含空間'屬於'空間'和'空間'也屬於一個'場地'。也許你認爲這種方式是因爲你在表格描述中沒有看到FK約束,那是因爲我通過padrino生成器創建了它們,我認爲他們沒有實現通過遷移創建外鍵的功能。然而,如果宣佈FK,唯一更快的查詢將是唯一的變化。順便說一句,如果你添加表結構將會很好。 – marmeladze