2012-09-10 55 views
0

我試圖建立一個連接如下:的Rails:加入多個表給表「table1.table2」不存在

UserLog.find(:all, :joins => " JOIN client_inspector ON user_log.COMPUTER_NAME = client_inspector.Retrieving_Hostname ", :select=> "DISTINCT user_log.COMPUTER_NAME, client_inspector.Retrieving_Hostname ") 

但它給我下面的錯誤 - >

ActiveRecord::StatementInvalid (Mysql2::Error: Table 'user_log.client_inspector' doesn't exist: SELECT DISTINCT user_log.COMPUTER_NAME, client_inspector.Retrieving_Hostname FROM `user_log` JOIN client_inspector ON user_ 

log.COMPUTER_NAME = client_inspector.Retrieving_Hostname):

我理解的MySQL錯誤,但爲什麼Rails的這樣做呢?

我沒有任何創建的模型將這兩個表關聯在一起。我想要做的是能夠通過不同於Ruby默認設置的列來加入。

有沒有其他方法可以做到這一點?

編輯:我更新包括我的實際表名稱。

回答

0

任何尋找這個問題的答案,就用它來抓住你的數據 - >

@returned_fields = ActiveRecord::Base.connection.execute(sql) 

這不會返回「Model」對象,而只是一個通用的MySQL :: Result對象。使用該工具來獲取數據 - >

@returned_fields.each 

這會給你一個數組的數組像這樣 - >

[[1,2], [3,4], etc...] 

此外,還要確保你有像這樣的數據庫名限定的表 - >

SELECT foo FROM database.BAR as bar JOIN database.BLEZ as blez ON bar.COMPUTER_NAME = blez.Retrieving_Hostname 

我的問題竟然是數據庫的限定條件。由於我沒有資格,它正在尋找錯誤的數據庫(我目前連接到的那個)的表。

0

mysql請求有效。 我不認爲你需要你的模型之間的關聯。在MySQL查詢

UserLog.find(:all, :joins => " JOIN client_inspectors ON user_logs.COMPUTER_NAME = client_inspectors.Retrieving_Hostname ", :select=> "DISTINCT user_logs.COMPUTER_NAME, client_inspectors.Retrieving_Hostname ") 

有了一個「S」每個表名稱

編輯:模型的

表名與一個複數化名稱創建的,可你試試這個更新與表名

+0

是的,這是等價的,但我不明白爲什麼它這樣做。我更新了包含我的實際表名。 – hacket

+0

事實證明,我需要將表格名稱限定爲數據庫{dot} table ... whodda thunk。 – hacket

相關問題