Rails 3 using MongoDB via the mongoid adapter - is there a way to share attribute specifications without using single-page inheritance?

Probably a confusing title, but not sure how else to express it. An example should be clearer. I have many different models that have the same attributes. Therefore, in each model, I must specify the same attributes, and THEN the attributes specific to this particular model.

Is it possible to create some class that lists these basic attributes and then inherit from this class without using single-page inheritance? Because if I put all the common attributes and included Mongoid in one model and inherited from this base model in other models, then the STI will be enforced and all my models will be saved in one mongodb collection, differentiated by the "_type" field.

This is what I have:

class Model_1 include Mongoid::Document field :uuid, :type => String field :process_date, :type => String ... end class Model_2 include Mongoid::Document field :uuid, :type => String field :process_date, :type => String ... end 

But this is the functionality that I use:

 class Base_model field :uuid, :type => String field :process_date, :type => String end class Model_1 < Base_model # To ensure STI is not enforced include Mongoid::Document # Attribute list inherited from Base_model end 

The problem is that if you do not have "enable Mongoid :: Document" in Base_model, then this base model does not know about the functionality of "field ...". But if you include mongoid in the base model and inherit it, the STI will be enforced.

I can’t do an STI for this specific situation, but it’s a code code to have several models, all with the same list of attributes indicated over and over (the number of models is growing and each share is about 15-20 attributes, so anytime when I have to change the name of the model, he takes a lot of effort to change it everywhere ...).

+3
mongodb ruby-on-rails-3 mongoid sti
source share
3 answers

You can define common attributes in the module and enable this.

 require 'mongoid' module DefaultAttrs def self.included(klass) klass.instance_eval do field :uuid, :type => String end end end class Foo include Mongoid::Document include DefaultAttrs field :a, :type => String end class Bar include Mongoid::Document include DefaultAttrs field :b, :type => String end 
+4
source share

I had the same question and wanted to start mixing first. However, after talking with some experts, it turns out that using mongoids unidirectional overlay (one collection for all children) can be a way, depending on your use case. Please see my post here: single collection versus separate collections for inherited objects

0
source share

You can associate them with a new module, i.e.

 module Mongoid module Sunshine extend ActiveSupport::Concern included do include Mongoid::Document include Mongoid::Timestamps include Mongoid::TouchParentsRecursively include Mongoid::Paranoia include Mongoid::UUIDGenerator end end end class School include Mongoid::Sunshine end 
0
source share

All Articles