【问题标题】:Run a ROR Model method in MongoDB shell在 MongoDB shell 中运行 ROR 模型方法
【发布时间】:2016-04-18 17:16:50
【问题描述】:

我的模型中有一个 ruby​​ 方法。我正在生产中测试该方法,并希望在 MongoDB shell 中运行该方法。运行模型中的 ruby​​ 方法的语法是什么。这是我的方法。

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 中运行此方法的方法。

【问题讨论】:

  • 从 Rails 控制台运行 normalize_code 并将结果粘贴到 MongoDB shell 中。或者在 JavaScript 中实现normalize_code,以便您可以在 MongoDB shell 中运行它。

标签: ruby-on-rails ruby mongodb shell


【解决方案1】:

您正在混合两种不同的东西。您的 Rails 模型是用 Ruby 语言编写的,可以在 Ruby 解释器中运行。

MongoDB shell 基本上是 MongoDB 实例的接口,它没有 Ruby 解释器。所以你不能在 MongoDB shell 中调用 Rails 模型方法

【讨论】:

猜你喜欢
  • 2011-08-11
  • 1970-01-01
  • 2015-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-21
  • 2016-02-24
  • 1970-01-01
相关资源
最近更新 更多