-
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
New records should load has_many relationships with custom primary keys #3748
New records should load has_many relationships with custom primary keys #3748
Conversation
Please could you add some tests to check the actual list of records returned, as well as checking the size? Also please use assert_equal, e.g. assert_equal 1, foo.size Not: assert foo.size == 1 |
Thanks for the tip on assert_equal. I added a check to that test to ensure the proper list of essays is returned. I assumed custom primary_key on a has_many was already under test coverage which wasn't true so I added a basic test for that case. If there is anything else you think should be added, please let me know. |
You should not commit the config.yml |
Sorry about that, good catch. |
Squashing extra commits is also a good practice :-) |
end | ||
|
||
def test_has_many_custom_primary_key | ||
david = authors(:david) |
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.
This is not actually testing the bug, as it's not a new record?!
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.
line 1404 author is never saved and is a new record. Should I move line 1404 to a separate test?
On line 1409, I added this test because the custom primary key functionality of a has_many was not under test coverage previously, even for the standard case of an existing record.
Made a couple more comments. Please also squash the commits as suggested above. Thanks. |
… is present but the record is unsaved
I squashed the commits and cleaned up the comments. Is there anything else needed? |
New records should load has_many relationships with custom primary keys
If a has_many relationship is defined with a custom primary key, a new, unsaved record with that key set will not load the association. This is because AR checks new_record? and assumes (correctly in normal situations) that an unsaved record couldn't possibly have any related records, so it skips the round trip to the database.
I believe this is an error, and the association should be loaded from the database.
This pull request fixes this issue. I tested the 4 big connection adapters and added 2 tests.
I overrode the foreign_key_defined? method in has_many_association to handle this since that seemed to be written almost specifically for this purpose in has_many_through, but if that isn't the best way to handle this I'm open to discussion.
Thanks