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.

models' respond_to? does not honor private

#959

I want to make an attribute truly private, so I tried this:

class Foo < ActiveRecord::Base
  # attribute bar
  private
  def bar
    read_attribute :bar
  end
  def bar=(value)
    write_attribute :bar, value
  end
end

but Foo.new.respond_to?(:bar) returns true, where a normal (non-active-record) instance would return false for private methods.

This patch adds a check in respond_to? to honor private methods that shadow attributes.

Reported by Gaius Centus Novus · September 2nd, 2008 @ 11:14 PM

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

Activity

  1. Gaius Centus Novus
    Gaius Centus Novus

    that patch doesn't honor the parameter include_priv. Perhaps the following would be even better?

    def respond_to?(method, include_priv = false)
      return true if super
      return include_priv if private_methods.include?(method_name)
      ...
    end
    
    

    September 3rd, 2008 @ 01:54 PM

  2. Gaius Centus Novus
  3. Pratik
    Pratik
    • Assigned user set to Pratik
    • State changed from new to wontfix

    Is this still a problem ? Also, this is a bit tricky. I think it's worth discussing in the core list.

    Could you please upload a patch against latest edge ? This one doesn't apply any longer.

    I'll reopen once we have these issues handled.

    Thanks.

    December 20th, 2008 @ 03:45 PM

  4. Pratik
    Pratik
    • State changed from wontfix to incomplete

    December 20th, 2008 @ 03:45 PM

  5. Prem Sichanugrist (sikachu)
    Prem Sichanugrist (sikachu)
    • State changed from incomplete to stale

    January 22nd, 2010 @ 08:52 AM

  6. bingbing