Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

in_scope?

#1463

The patch adds new scope functionality that I've found very useful in my project. It simply adds new instance method called in_scope? to each defined named_scope. I use it in several cases, i.e.


class User
  named_scope :destroyable, :conditions => "..." # simple association check
  before_destroy :in_destroyable?
end

user = User.destroyable.first

user.in_destroyable? # the same as User.destroyable.include?(user)
#=> true

Firstly I used :destroyable? method (without "in_") but it may collide with some boolean attributes like "active", etc. Implementation is very simple and it uses ordinary :include? method to check the inclusion. Probably it can be optimised to do the inclusion check in SQL, but for me it's really enough.

Reported by qoobaa · November 24th, 2008 @ 10:59 PM

State: invalid
Milestone: 2.x
Assigned to: Pratik Pratik
Importance: none

Activity

  1. Pratik
    Pratik
    • Assigned user set to Pratik
    • Tag changed from activerecord, edge, enhancement, named_scope, tested to activerecord, edge, enhancement, named_scope, patch, tested
    • State changed from new to invalid

    I don't think this belongs to core. Also, you could just use exists? though.

    Thanks.

    March 7th, 2009 @ 03:58 PM