2017-05-27 14 views
0

(我已經看到了這一切相關問題,但他們都沒有工作對我來說)導軌 - 其中:created_at比self.created_at以下(以上)(Mongoid)

before_update,我需要取之前的記錄自我但無法弄清楚如何查詢「created_at < self.created_at」。我想這樣的:

def set_update_consumo 
    lastRecord = 
     Reporte.where(:hidrometro_id => self.hidrometro_id) 
     .where('created_at < ?', self.created_at) 
     .order_by(:created_at => 'desc').offset(1).first 

    set_record(lastRecord) 
end 

而且我得到這個錯誤:如果我刪除where('created_at < ?', self.created_at)它的工作原理

Completed 500 Internal Server Error in 784ms

ArgumentError (wrong number of arguments (given 2, expected 1)):

app/models/reporte.rb:49:in 'set_update_consumo'

app/controllers/reportes_controller.rb:57:in `block in update'

app/controllers/reportes_controller.rb:56:in `update'

。 SO中沒有任何其他答案爲我工作,並且不能使用寶石(如squeel)。

我正在使用Rails 5.0.2(Ruby 2.4)與Mongoid

+0

你有沒有名爲'created_at'的方法? –

+0

你正在使用ActiveRecord而不是Mongoid的權利? – max

+0

你能從錯誤的堆棧跟蹤中至少粘貼幾行嗎? – BoraMa

回答

2

在Mongoid中,where語法與ActiveRecord有一點不同,實際上只採用一個參數 - 一個散列。對於「小於」表達你需要使用.lt方法的屬性符號上:

where(:created_at.lt => self.created_at) 

更多信息,請參見docs

此外,您可能不必在此處使用self,而簡單的created_at應該給出相同的結果。

+0

不能相信以前沒有檢查Mongo文檔..按預期工作,謝謝! –