-
Notifications
You must be signed in to change notification settings - Fork 899
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
fix issue in #reify_has_many_through #596
Conversation
Thanks for the PR! Can you add a test that demonstrates the issue? |
Just to clarify, we know that #590 is a confirmed bug, but it'd still be nice to have a test. Thanks. |
Ya sure. Will work on it. |
You should be able to use the failing test I already wrote (https://github.com/jaredbeck/pt_issue_590/blob/de54892426966d146a13c8c2382735826aa76660/spec/model/document_spec.rb) as a starting point. |
d4e4271
to
aaac0ea
Compare
@chapter.sections.first.update_attributes :name => "section 3" | ||
end | ||
|
||
context "when reified is called" do |
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.
The method is called reify
, not reified
. Please update the context string here.
@theRealNG this is looking good. I appreciate how thorough you were with the tests. My comments are mostly about style. I'm trying my best to understand the meat of the change, but I'd really like to get Ben's opinion as he is more familiar with the reify functions. CC: @batter |
Bug: Fix: Explanation with example :-
But i'm calling "reify_has_manys" on each section so that we can handle any levels of nesting. |
return | ||
end | ||
|
||
collection_keys = through_collection.map { |through_model| through_model.send(assoc.association_foreign_key)} |
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.
If I revert the change to this one line, by calling assoc.foreign_key
instead of assoc.association_foreign_key
, all the tests still pass. So, is it necessary to change this line?
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.
In the example of Document, Sections and paragraphs:
The through_model is a object of Section.
What we are trying to do is get the list of all the paragraphs associated to the section by calling:
section.paragraph_ids
If we do Section.reflect_on_association(:paragraphs).foreign_key, it returns 'section_id' and if we do Section.reflect_on_association(:paragraphs).association_foreign_key, it returns 'paragraph_id'. So we should be using 'association_foreign_key' instead of 'foreign_key'.
This was not an issue so far because in a 'belongs_to' association assoc.association_foreign_key and assoc.foreign_key are the same.
Even if you replace assoc.association_foreign_key with assoc.foreign_key it works because there is no chance of 'has_many' associations to hit the line because we are calling 'return' on line:424
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.
That all makes sense, thanks.
Ben, I think this is ready to merge, now that it includes theRealNG#1 What do you think? |
Do you want me to squash the commits into a single commit? |
Sure, thanks. |
e0fd33e
to
a660615
Compare
👍 Nice work, thanks to both of you. My take is it's fine to leave it as a split commit as long as the merge has no fast forward but I don't care |
@jaredbeck Did the test we wrote ever end up working out of curiosity? |
Yes, see theRealNG#1 |
fix issue in #reify_has_many_through
So what is our merge process now for these types of things @jaredbeck? Merge to |
Yes, that's how I picture it working. I think it's a common strategy; it's what rails seems to do. One thing I'm not sure of is how to run CI for a backport. I guess we could do a PR for each backport.. |
ok that sounds good, just wanted to be clear about how you were envisioning it |
Fix for #590
The assoc.foreign_key holds true only when the association is as follows
In this case the assoc.foreign_key and assoc.association_foreign_key are the same
But in the case of the following associations
the assoc.foreign_key and assoc.association_foreign_key are different and assoc.association_foreign_key gets the correct list of associated foreign_keys for the model.
For Ref: http://www.rubydoc.info/docs/rails/3.0.0/ActiveRecord%2FReflection%2FAssociationReflection%3Aassociation_foreign_key
EDIT:
When a nested has_many relation is present, using the "reify_has_manys" method to reify the has_many through associations.