-3

When i write a Controller Class, it's possible to use methods like:

@article.update(article_params)

The method update comes from the class ActiveRecord::Base : http://api.rubyonrails.org/v2.3/classes/ActiveRecord/Base.html#M001966

My questions:

  1. How can I use the update method from a Controller class that does not inherit from ActiveRecord::Base? It looks like ApplicationController inherits only from ActionController::Base

  2. Are there any resources available to read up on this?

Thanks for help, Cheers B

philip yoo
  • 2,462
  • 5
  • 22
  • 37
  • 5
    Your mixing apples and pears and this question makes no sense at all. `ActiveRecord::Base` is models, `ActionController::Base` is controllers. `@article.update(article_params)` is calling `.update` on a model instance and has nothing to do with `ApplicationController`. You might want to start by reading: http://guides.rubyonrails.org/getting_started.html – max Feb 17 '16 at 23:57
  • 1
    To be honest, `ActiveRecord::Base` is only used for models in a database. There is a lot of extensive documentation over how both of `ActionController` and `ActiveRecord` works. I also think it's not the **proper** question to ask because there are a lot of people keep asking the same question over and over again. what @max said, read the documentation from Rails itself. – Rod Argumedo Feb 18 '16 at 01:51

1 Answers1

2

Rails follow the MVC architecture. In that architecture, your models interact with the database and your controllers interact with incoming requests (usually by orchestrating some models and then passing those off to some views that render the content). Rails achieves reuse through inheritance, so you can't (and probably wouldn't want to) mix concerns

I recommend checking out the official Rails guides. I think it's some of the best written documentation in the industry:

http://guides.rubyonrails.org/

Josh Bodah
  • 1,223
  • 6
  • 17
  • Hi, MVC is totally clear, the meaning is, how is it possible to use the update() -methode within the controller, because it is a class with a scope. And I don't mixup everything, method is using in the controller -> (http://guides.rubyonrails.org/action_controller_overview.html) .... My question is, how it works inside rails. how is it possible to use the method in the controller, from where comes that. – DeveloperB Feb 18 '16 at 10:36
  • You could do something like `MyModel.where(id: params[:id]).update(name: "hello")` in your controller method. You create a controller method hen that controller method delegates to the model class – Josh Bodah Feb 19 '16 at 12:04