Looking through the code we’ve built so far, I see a fair degree of flexibility in where we define and call methods:
A public class method (self.find_products_for_sale) defined in one model (product.rb) and called in another model altogether (store_controller.rb)
# A public class method (initialize/Cart.new) defined in one model (cart.rb) and called in a controller with an unrelated name (store_controller.rb)
# A public instance method (increment quantity) defined in one model (cart_items.rb) and called in another model altogether (cart.rb)
# A public instance method (add_product) defined in one model (cart.rb) and called in controller with an unrelated name (store_controller.rb)
# A public instance method (add_to_cart) defined in a controller (store_controller.rb) and called in a view with a related path (store/index.html.erb)
# A protected instance method (price_must_be_at_least_a_cent) defined in a model (product.rb) and called in the same model
I’m looking for patterns here. So far I’m seeing:
To make a public method available to a view, define it in the controller with the related name
# To make a public method available in any model or controller, define it in any model
# To make a private method available to a model, define it in that model
# No difference between class and instance methods
Correct? Why? Is your answer affected by the fact that, at this stage in the book, we’ve not yet touched on associations or routing?
If it’s relevant to your answer, why are there not carts and cart_items controllers when we have a carts and cart_items models?
Many thanks in advance,