-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
Add support for unpersisted CPK has_one/has_many through associations #48552
Merged
+26
−2
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -86,7 +86,9 @@ def stale_state | |||||
end | ||||||
|
||||||
def foreign_key_present? | ||||||
through_reflection.belongs_to? && !owner[through_reflection.foreign_key].nil? | ||||||
through_reflection.belongs_to? && Array(through_reflection.foreign_key).all? do |foreign_key_column| | ||||||
!owner[foreign_key_column].nil? | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above
Suggested change
|
||||||
end | ||||||
end | ||||||
|
||||||
def ensure_mutable | ||||||
|
This file contains 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
This file contains 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
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should be using a
_read_attribute
here for cases where a composite foreign key containsid
column asowner["id"]
returns a whole identifier and not just value of theid
columnThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense. I'll see if I can add a regression test for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thinking about it a little more, I can't see a reason for the foreign key to ever be id. That is what primary keys are named. I suppose we can support this use-case for completeness if you feel strongly about it. Testing might be easier once I change
Cpk::Book
to have an id instead of number in #48564.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually you are right, having
:id
as part of the foreign key would be a very unconventional scenario, can't say I can come up with a meaningful use-case. Let's not rush with switching to_read_attribute
then until we have a meaningful reproduction.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right now, the only reason I can see to use
_read_attribute
here is to bypass alias checking. Seerails/activerecord/lib/active_record/attribute_methods/read.rb
Lines 29 to 34 in ba9c2e5
rails/activerecord/lib/active_record/attribute_methods/read.rb
Lines 44 to 49 in ba9c2e5
Do we think this is necessary?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eileencodes Nikita and I chatted about this and we agree it isn't necessary anymore to use
_read_attribute
, so I'm going to merge as-is. I'm happy to revert/fix forward in another patch if you feel strongly this use-case should be covered, but the foreign key columns should never useid
, and I don't want to further change behaviour of associations if we aren't proving value.