0

I am looking at a sample Rails application and see some strange things. Well just strange to me because my past experience was with C#.

So in the ApplicationController I have a "private" method like this:

 private

    def current_cart 
      Cart.find(session[:cart_id])
    rescue ActiveRecord::RecordNotFound
      cart = Cart.create
      session[:cart_id] = cart.id
      cart
    end

and then in orders_controller class I have another method that in its body it is saying something like:

def new
   @cart = current_curt
// ....
end

What happened ? It was private but we can access it? And we don't need to create an instance of it before accessing it ? Can someone talk a little bit about how the methods in controllers work together in Rails?

sjain
  • 23,126
  • 28
  • 107
  • 185
Bohn
  • 26,091
  • 61
  • 167
  • 254
  • 3
    Check out this question: http://stackoverflow.com/questions/3534449/why-does-ruby-have-both-private-and-protected-methods . Basically `private` methods are available to subclasses and it acts more like `protected` in other languages. – Marc Baumbach Jan 22 '13 at 15:14

1 Answers1

2

There is an instance of the controller, instantiated by the framework, per-request.

And yes, subclasses can access the method (as running it would show).

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • ah good call, I hadn't noticed at the top it is saying CartsController is inheriting from ApplicationContorller. – Bohn Jan 22 '13 at 15:14