Rails - root or application model

I just looked at the rails and noticed that there is an application controller, but no application model.

Is there a root model in the rails? if not, where do you put the code snippet that should be in each model.

Thanks, Alex

+5
source share
3 answers

It doesn’t mean that your controllers should subclass ApplicationController, but this is usually a standard, because the vast majority of rail applications use features layout(which may vary for each controller), so instead of forcing rare ones without layouts from expanding the layout ( layout nilor layout false) for each controller, they create an "abstract" that allows you to easily include controller functions for the entire application.

Now for models, you can create a ApplicationModelsubclass for all of your models, but there are two things to think about:

  • ActiveRecord usually detects when you subclass already subclasses ActiveRecord::Baseand uses this to enable STI (unidirectional table inheritance).
  • ApplicationModel , , , . .

, abstract_class true ActiveRecord.

class ApplicationModel < ActiveRecord::Base
  self.abstract_class = true
end

ActionController, abstract_class true, , , ApplicationModel. ApplicationController , .

+13

rails ActiveRecord:: Base, , , .

ActiveRecord:: Base lib ( rails), , , (). .

+2

I think that I remember a few years ago, in fact, there was an ann AppModel by default. Is this true or am I mixing something (I have not worked with Rails for several years so far)?

-1
source

All Articles