Support relationships between classes with a common module parent#576
Open
joshmenden wants to merge 2 commits intoDynamoid:masterfrom
Open
Support relationships between classes with a common module parent#576joshmenden wants to merge 2 commits intoDynamoid:masterfrom
joshmenden wants to merge 2 commits intoDynamoid:masterfrom
Conversation
joshmenden
commented
Aug 8, 2022
| # | ||
| # @since 0.2.0 | ||
| def target_association | ||
| has_many_key_name = options[:inverse_of] || source.class.to_s.underscore.pluralize.to_sym |
Author
There was a problem hiding this comment.
In every other file the pluralize comes before the underscore, so I changed this to be consistent
andrykonchin
reviewed
Aug 8, 2022
| if target_class.module_parent != Object && (target_class.module_parent == source.class.module_parent) | ||
| has_many_key_name = source.class.to_s.demodulize.pluralize.underscore.to_sym | ||
| has_one_key_name = source.class.to_s.demodulize.underscore.to_sym | ||
| end |
Member
There was a problem hiding this comment.
Can we unconditionally demodulize a class name?
Member
|
Could you plz add specs for the fixed behaviour? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I came about this problem because I am using Dynamoid in a Rails Engine, and I discovered issues where the relationships between models were not functioning correctly due to the
isolate_namespacefunctionality of engines.For example, piggybacking off the example in the README, if I had classes like this, but in an engine named
MyEngine...I would get the following in a rails console
In other words, the
MyEngine::AddressID would not be saved on theMyEngine::Userdynamo record.This is because in this line of code in
belongs_to.rb, theMyEngine::Addressclass would evaluate to:"my_engine/addresses"which of course is incorrect.My solution is to check if both the
target_classand thesource_classshare the samemodule_parent, check that that module parent custom to the app, and then use thedemodulizefunctionality to strip the class down to its bare name.