Zhao Li
Zhao Li

Reputation: 5716

ActiveRecord query for FALSE attributes

I'm not sure what the correct terminology is so my Googling didn't help much.

But anyways, ActiveRecord allows you to search all records with a boolean attribute TRUE with something like this:

@current_events = Event.current

So all events with attribute "current" equals TRUE is returned.

Is there a nifty ActiveRecord method to return all that is FALSE? Maybe something like:

@outdated_events = Event.not_current

?

If not, then I can just use the "where" method. But I was just curious.

Thanks!

Upvotes: 0

Views: 267

Answers (1)

Mori
Mori

Reputation: 27789

You may be mistaking ActiveRecord 'named scopes' class methods for a Rails feature. For instance,

class Foo < ActiveRecord::Base
  def self.current
    where current: true
  end
end

will create the behavior you describe, but not just calling Foo.current without the above. So to create not_current, yep, use where:

def self.not_current
  where current: false
end

Upvotes: 1

Related Questions