Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements polymorphic associations (Closes #51) #275

Merged
merged 1 commit into from
Jun 15, 2022

Conversation

zedtux
Copy link
Collaborator

@zedtux zedtux commented Feb 27, 2021

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 zedtux added the feature New functionalities label Feb 27, 2021
@zedtux zedtux requested a review from nviennot February 27, 2021 09:05
@zedtux zedtux self-assigned this Feb 27, 2021
@zedtux
Copy link
Collaborator Author

zedtux commented Feb 27, 2021

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 zedtux force-pushed the features/polymorphic-association branch from e435d90 to 23de418 Compare February 28, 2021 12:31
@zedtux
Copy link
Collaborator Author

zedtux commented Mar 7, 2021

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

@zedtux zedtux force-pushed the features/polymorphic-association branch from 23de418 to 90a9944 Compare March 14, 2021 08:00
@zedtux zedtux force-pushed the features/polymorphic-association branch from 1411805 to 009c340 Compare September 10, 2021 05:19
@zedtux
Copy link
Collaborator Author

zedtux commented Nov 8, 2021

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

@zedtux zedtux force-pushed the features/polymorphic-association branch from df8bc0e to 4b2dccd Compare November 9, 2021 20:24
@zedtux zedtux force-pushed the features/polymorphic-association branch from 4b2dccd to 5d5f281 Compare May 31, 2022 20:23
@zedtux zedtux force-pushed the features/polymorphic-association branch 2 times, most recently from 57d98f4 to 1f58103 Compare June 15, 2022 19:49
@zedtux zedtux force-pushed the features/polymorphic-association branch from 1f58103 to 3139e3f Compare June 15, 2022 19:51
@zedtux zedtux merged commit 594b86f into master Jun 15, 2022
@zedtux zedtux deleted the features/polymorphic-association branch June 15, 2022 19:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New functionalities
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants