NoBrainerORM / nobrainer

Ruby ORM for RethinkDB
http://nobrainer.io/
Other
387 stars 49 forks source link

Implements polymorphic associations (Closes #51) #275

Closed zedtux closed 2 years ago

zedtux commented 3 years ago

This PR implements the polymorphic associations, the same way ActiveRecord does.

belongs_to now accepts the polymorphic: true and has_many/has_one the as: :xxx. Nobrainer adds 2 fields to the polymorphic model underthehood to store the foreign ID and Class then reuse them in query criteria.

zedtux commented 3 years ago

This PR adds automatically 2 new fields which should have an index. Doing so would raise the error:

NoBrainer::Error::MissingIndex: Please run `NoBrainer.sync_indexes' or `rake nobrainer:sync_indexes' to create the index `imageable__id__imageable_type` in the table `nobrainer_test.pictures`. Read http://nobrainer.io/docs/indexes for more information.

This is completely normal and actually works like that with ActiveRecord when generating the migration script.

zedtux commented 3 years ago

Thank you for all comments, I'll rework that soon. 🙇

zedtux commented 2 years ago

The following case doesn't seem to work:

    define_class :Metadata do
      include NoBrainer::Document
    end

   define_class :Picture, Image do
      include NoBrainer::Document
      has_many :metadatas
    end

    define_class :Event do
      include NoBrainer::Document

      belongs_to :restaurant
      has_many :photos, as: :imageable, class_name: 'Picture'
      has_many :metadatas, through: :photos
    end

Trying to access event.metadatas fails with:

Traceback (most recent call last):
        1: from (irb):1
NoMethodError (undefined method `metadatas' for #<Picture:0x00007f8eed0fcb38>)
Did you mean?  photos