rails/rails — Issue #4264: "only" AR::relation command don't bind to scope if defined inside method
Author: daniel2d2art
Created: 2012-01-02 19:17:25+00:00
State: open

You can use:

MyModel.where('foo = 1 AND bar = 5').select('foo, bar, created_at, updated_at).order('created_at ASC').limit(10).only(:where).select(:id)

Will result:
select id from mymodels where foo = 1 and bar = 5

Or you can have for same results your method for scoping:

class MyModel < ActiveRecord
def self.my_scope_method
only(:where).select(:id)
end
end

MyModel.where('foo = 1 AND bar = 5').select('foo, bar, created_at, updated_at).order('created_at ASC').limit(10).my_scope_method

But then obtains ...

NoMethodError: undefined method `only' for #

You can workaround by:

def self.my_scope_method
where('1=1').only(:where).select(:id)
end

But not nice