2016-07-20 41 views
0

我遵循Frank Rietta關於「爲SQL視圖添加Rake任務到Rails項目」的優秀post。我喜歡他關於rails中數據庫視圖的觀點和他的干涉方法。Rails如何將解析數據從sql視圖傳遞到模型?

我能夠做的rake db:views並創建我的觀點,但我沒能獲得在模型中的信息,這是我models/reports/revenue.rb

class Report::Revenue < ApplicationRecord 
    self.table_name = 'report_revenues' 
end 

我改變了擴展,因爲我使用Rails 5.0。 0

如果我執行rails console --sandbox有我執行Report::Revenue我得到以下

2.3.1 :004 > Report::Revenue 
NameError: uninitialized constant Report 

我不知道我缺少什麼

+1

您是否將報告文件夾添加到autoload_paths?如果沒有,嘗試添加'config.autoload_paths + =%W(#{config.root}/app/models/reports)'到'config/application.rb' – user3033467

+0

謝謝@ user3033467我試過了,但它沒有工作,但它幫助我意識到問題在於我在拼寫錯誤中報告在revenue.rb文件中,我應該編寫Reports。 – agusgambina

回答

0

Rails希望模塊名稱和文件夾名稱匹配。請注意,你混合使用單數和複數。

這就是說:你有你的模式更改爲:

class Reports::Revenue < ApplicationRecord 
    self.table_name = 'report_revenues' 
end 

或者模型轉移到一個名爲models/report/revenue.rb文件夾。

+0

謝謝你,就是這樣 – agusgambina

相關問題