ActiveRecord inheritance with various database tables

I just started to learn how to use more advanced models in Rails. The one I use regularly, with great success, is a model in which a cross-to-many-to-many cross-reference relationship is implemented by a class that itself is a subclass of the base class in a many-to-many relationship.

Thus, the cross-reference class can act as a stand-in for the base class.

A good example is that the node (NavigationNode) navigation hierarchy cross-references the user role. At the cross-reference point, the class (RoleNavigationNode) can inherit from NavigationNode and still have in-depth knowledge of the user's role.

My question (in the above case) can RoleNavigationNode inherit from NavigationNode and nevertheless refer to the cross-reference table, and not to the one accessed by NavigationNode - this, of course, is using ActiveRecord.

I have not investigated polymorphic association, which may be more appropriate.

Thanks in advance,

+3
source share
2 answers

tried set_table_namein a subclass?

Also study customization @abstract_classin model classes.

Finally, what you need may just be the Mixin that you include in both models.

, , , -ActiveRecord-ish. , , , - .

+2

Rails 3:

class Common < ActiveRecord::Base
  @abstract_class = true
  def common
    "Foobar!"
  end
end

class Model < Common
end

class AnotherModel < Common
end

abstract_class Rails commons .

+2

Source: https://habr.com/ru/post/1707080/


All Articles