2016-04-18 57 views
-2

我的模型中有一個ruby方法。我正在測試生產中的方法,並希望在MongoDB shell中運行該方法。運行模型中的ruby方法的語法是什麼?這是我的方法。在MongoDB shell中運行ROR模型方法

Coupon.rb 

def self.get(code) 
    where(
    :code => (normalize_code(code)), 
    :$and => [ 
     { 
     :$or => [ 
      { :coupon_count.gte => 1 }, 
      { :coupon_count => nil } 
     ] 
     }, { 
     :$or => [ 
      { :expires_at.gte => Date.today }, 
      { :expires_at => nil } 
     ] 
     } 
    ] 
).first 
end 

我似乎無法找到一種方法在MongoDB Shell中運行此方法。

+0

從Rails控制檯運行'normalize_code'並將結果粘貼到MongoDB shell中。或者在JavaScript中實現'normalize_code',這樣就可以在MongoDB shell中運行它。 –

回答

2

你在混合2種不同的東西。你的Rails模型是用Ruby語言編寫的,可以在Ruby解釋器中運行。

MongoDB shell基本上是MongoDB實例的接口,它沒有Ruby解釋器。所以你不能調用在MongoDB shell中的Rails模型方法

+0

好的謝謝。這可以解釋爲什麼我的原始問題有問題。請看看這個,讓我知道如果你看到什麼是錯的。 http://stackoverflow.com/q/36677603/3877058?sem=2 – SupremeA