does not work within named_scope block


Trying to create a named_scope that would check that the record is not

I have companies and contacts, where a contact can be a primary
contact for a company:

class Company
has_many :contacts

class Contact < ActiveRecord::Base
belongs_to :company

named_scope :primary, :conditions => { :is_primary => true } do
def mark_as_non_primary!
each { |p| p.update_attribute(:is_primary, false) }

named_scope :but_not_itself, :conditions => [‘ != ?’,

So I would like to do something like this from Contact model:


However this does not work because id method within but_not_itself
named_scope returns an (instance id) and not ActiveRecord id
(database id).

How do I make it work?


On Aug 18, 11:21 pm, Swartz [email protected] wrote:

named_scope :but_not_itself, :conditions => [‘ != ?’,

The reason this does not work is because that expression “id” is
evaluated when named_scope is call to create the scope, ie when the
class is loaded, so self is the class Contact.
It’s the same reason why doing :conditions => [‘created_at < ?’,] won’t work properly: the is evaluated once, at
class load time and then never again.

Luckily instead of passing a hash of options you can pass a lambda
that returns a hash of options. Since that proc will be called when
the scope is loaded and in the right context you should be fine.


Hi Fred,

Thanks for your input.

So far I changed that named_scope into this:
named_scope :but_not_itself,
lambda { |id| { :conditions => [‘ != ?’, id]} }

Is that what you meant?

Is there a better way to do this?

Now I have to do this:

seems, to me at least, kind of ugly having to pass… :slight_smile:

By the way my inspiration for this came from
Where the author has User.inactive.activate to very neatly activate
inactive users.


On Aug 18, 5:04 pm, Frederick C. [email protected]