views:

25

answers:

1

Hi everyone,

I need a way to make association collection methods (particularly the append <<) private. Here's an example:

class Foo < ActiveRecord::Base
  has_many :bars

  def add_bar (bar)
     # does something extra first
     # but still also use the <<, ultimately
     bars.send(:<<, bar)
  end
end

Basically, i do not want any part of the application to be using << by itself, i need it to go through the "add_bar" method. Any suggestions?

Thank you very much!

+1  A: 

There's private_class_method (didn't know that myself :)). You can try something along the lines of

class Foo < ActiveRecord::Base
  has_many :bars do
    private_class_method :<<
  end

  def add_bar (bar)
     # does something extra first
     # but still also use the <<, ultimately
     bars.send(:<<, bar)
  end
end

Didn't test that, see if it works.

neutrino
@neutrino - thanks for the reply! that doesn't seem to work. i am getting a "undefined method `<<' for class `Module'" error. I also tried just "private :<<" and got the same error. thanks for the help!
janechii