Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-2130

Mixing Polymorphic and Inverse Realtions

    • Type: Icon: Task Task
    • Resolution: Done
    • 2.5.0
    • Affects Version/s: None
    • Component/s: None
    • None

      I believe I have these the relationships set up right, The note is getting the created_by relationship bit for some reason authored notes comes up empty for the user. This could be do to some of the inheritance I am doing. Please take a look and tell me what I am doing wrong.

      # The note model
      class Note
        include Mongoid::Document
        include Mongoid::Timestamps
      
        # Attr Accessible
        attr_accessible :created_by, :updated_by, :body, :noted_on
      
        # Relationships
        belongs_to :noted_on, polymorphic: true
        belongs_to :created_by, class_name: 'User', inverse_of: :authored_notes
        belongs_to :updated_by, class_name: 'User', inverse_of: :authored_notes
      
        # Fields
        field :body, type: String
      
        # Validations
        validates :body, :firm_id, :created_by_id, presence: true
      
      end
      
      # Contacts can be noted on
      class Contact
        include Mongoid::Document
        include Mongoid::Timestamps
        include Mongoid::MultiParameterAttributes
      
        attr_accessor :client_ids, :matters, :historical_name
      
        attr_accessible :prefix, :suffix, :middle_name, :first_name, :last_name, :birthday, :email_addresses_attributes, :phone_numbers_attributes, :personal_relationships_attributes, :addresses_attributes, :_type
      
        belongs_to :firm
      
        # Validations
        validates :firm_id, :first_name, :last_name, presence: true
      
        # Notes
        has_many :notes, as: :noted_on, dependent: :destroy
      
        #Fields
        field :prefix       , type: String
        field :first_name   , type: String
        field :middle_name  , type: String
        field :last_name    , type: String
        field :suffix       , type: String
        field :birthday     , type: Date
        field :_id          , type: String, default: ->{ SecureRandom.hex(5) }
      
        # The full_name
        def full_name
          [prefix, first_name, middle_name, last_name, suffix].select(&:present?).join(" ").titleize
        end
      
      end
      
      # Users are a subclass of contact so they can be noted on AND they can author notes
      class User < Contact
        include ActiveModel::SecurePassword
      
        attr_accessible :password, :password_confirmation, :settings
      
        has_many :authored_notes, class_name: "Note", inverse_of: :created_by
        has_many :authored_notes, class_name: "Note", inverse_of: :updated_by
      
        # User Specific Fields
        field :password_digest, type: String
        field :login_token, type: String
        field :user_roles, type: Array, default: -> { %w(Attorney) }
      
        # Active Model, active record
        has_secure_password
      
        # Validations
        validates :password, :password_confirmation, presence: true, length: { minimum: 6 }, on: :create
      
      end
      
      
      

      Other items can be noted on as well, contacts provides just one example.

            Assignee:
            Unassigned Unassigned
            Reporter:
            jwaldrip Jason Waldrip
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: