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.

ActiveRecord - named_scope using ':columns' as the name is buggered

#4167

I spent all afternoon on this one but it's rewarding to find it....

Given a model called "Article"

I have a list of named_scope directives as follows:

  named_scope :active,  :conditions => {:active     => true}
  named_scope :journal, :conditions => {:department => 0}
  named_scope :features,:conditions => {:department => 1}
  # NOTE: If you use :columns here, it's buggered!
  named_scope :column,  :conditions => {:department => 2}
  named_scope :news,    :conditions => {:department => 3}
  named_scope :tips,    :conditions => {:department => 4}

If I use :column for a name, it works fine. If I use :columns for a name the model's attributes are not accessible in script/console or when rails is running normally.

-Art

Reported by FlyboyArt · March 13th, 2010 @ 02:56 PM

State: resolved
Milestone: none
Assigned to: Ryan Bigg Ryan Bigg
Importance: none

Activity

  1. Ryan Bigg
    Ryan Bigg

    columns is a reserved class method on ActiveRecord. Perhaps named_scope should error if you try to define a method that is already defined?

    March 14th, 2010 @ 02:11 AM

  2. David Trasbo
    David Trasbo
    • Assigned user set to Ryan Bigg

    In edge Rails you will be warned if defining a scope results in a method being overridden. This ticket can be closed.

    April 11th, 2010 @ 09:19 PM

  3. Ryan Bigg
    Ryan Bigg
    • State changed from new to resolved

    Fixed in edge Rails:

        if !scopes[name] && respond_to?(name, true)
          logger.warn "Creating scope :#{name}. " \
                      "Overwriting existing method #{self.name}.#{name}."
        end
    

    April 11th, 2010 @ 09:32 PM