Search code examples
ruby-on-railsscopeundefined-function

Rails 4 chaining scope


I would like to reuse a scope I defined, but it returns an array.

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

When I call the function findNextAuctions I get this error:

  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>'

Solution

  • Rails doesn't have an OR statement. You can write SQL directly though so something like

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