【问题标题】:Rails 4 chaining scopeRails 4 链接范围
【发布时间】:2014-12-05 20:42:54
【问题描述】:

我想重用我定义的范围,但它返回一个数组。

scope :currents, -> { (where('started_at < NOW()') | where('started_at' => nil)) & where('ended_at > NOW()') & where('published_at < NOW()') }

def self.findNextAuctions()
  currents.order(ended_at: :asc).limit(3)
end

当我调用 findNextAuctions 函数时,我收到此错误:

  1) Error:
AuctionTest#test_should_fint_next_auctions:
NoMethodError: undefined method `order' for #<Array:0x007fae5d6e3878>
    app/models/auction.rb:13:in `findNextAuctions'
    test/models/auction_test.rb:14:in `block in <class:AuctionTest>'

【问题讨论】:

    标签: ruby-on-rails scope undefined-function


    【解决方案1】:

    Rails 没有 OR 语句。您可以直接编写 SQL,例如

    where("(started_at < NOW() or started_at = null) and ended_at > NOW() and published_at < NOW()")
    

    【讨论】:

      猜你喜欢
      • 2013-07-15
      • 2012-09-23
      • 2016-07-11
      • 1970-01-01
      • 2016-08-23
      • 2016-04-29
      • 1970-01-01
      • 2014-08-26
      • 2016-03-21
      相关资源
      最近更新 更多