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

New records should load has_many relationships with custom primary keys #3748

Merged
merged 1 commit into from
Nov 27, 2011

Conversation

samsonasu
Copy link
Contributor

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.

class Author < ActiveRecord::Base 
  has_many :essays, :primary_key => :name, :as => :writer
end

Author.new(:name => "David").essays # => []
                                    # => Should return [#<Essay ...>]

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

@jonleighton
Copy link
Member

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

@samsonasu
Copy link
Contributor Author

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.

@vijaydev
Copy link
Member

You should not commit the config.yml

@samsonasu
Copy link
Contributor Author

Sorry about that, good catch.

@arunagw
Copy link
Member

arunagw commented Nov 25, 2011

Squashing extra commits is also a good practice :-)

end

def test_has_many_custom_primary_key
david = authors(:david)
Copy link
Member

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?!

Copy link
Contributor Author

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.

@jonleighton
Copy link
Member

Made a couple more comments. Please also squash the commits as suggested above. Thanks.

@samsonasu
Copy link
Contributor Author

I squashed the commits and cleaned up the comments. Is there anything else needed?

jonleighton added a commit that referenced this pull request Nov 27, 2011
New records should load has_many relationships with custom primary keys
@jonleighton jonleighton merged commit b00cf12 into rails:master Nov 27, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants