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

Preserve Array#take(n) behaviour of HasManyAssociation #19105

Merged
merged 1 commit into from
Mar 2, 2015

Conversation

amatsuda
Copy link
Member

I'm not sure whether this behaviour change was by design, so let me PR.

take method with an argument had been available on HasManyAssociation until 2b63a35. It used to fallback to Array#take(n).
But now association.take(n) raises ArgumentError: wrong number of arguments (1 for 0).

This patch retrieves Array#take(n) behaviour without breaking the AR version of take.

Note that I didn't touch take! method because Ruby Array does not have take!.
So take can take an argument but take! can not take an argument after this change.
I think it's acceptable here, but I'd mention that the API would look a little bit inconsistent.

@amatsuda
Copy link
Member Author

@rafaelfranca Please take a 👀

def test_taking_with_a_number
assert_equal [posts(:misc_by_bob)], authors(:bob).posts.take(1)
assert_equal [posts(:misc_by_bob), posts(:other_by_bob)], authors(:bob).posts.take(2)
authors(:bob).posts.to_a
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You want to assert prior to this line that authors(:bob).association(:post) is not loaded. Otherwise you are not testing the "not loaded" code path.

Or for a more black box approach you can do:

assert_queries(1) do
  authors(:bob).posts.to_a
end

Which will ensure to_a makes the load happen rather than something earlier.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comment. Just made it more comprehensible and push -fed.

sgrif added a commit that referenced this pull request Mar 2, 2015
Preserve Array#take(n) behaviour of HasManyAssociation
@sgrif sgrif merged commit e20dc1b into rails:master Mar 2, 2015
sgrif added a commit that referenced this pull request Mar 2, 2015
Preserve Array#take(n) behaviour of HasManyAssociation
@amatsuda amatsuda deleted the array_take branch March 2, 2015 16:56
@arthurnn
Copy link
Member

arthurnn commented Mar 9, 2015

I guess this should be backported to 4.1.x and 4.2.x , as this was broken on those branches

@arthurnn
Copy link
Member

arthurnn commented Mar 9, 2015

I backported this to 4-1-stable, and to 4-1-10 rc, and 4-2-1 rc. (this was on 4-2-stable already)
cc @rafaelfranca

mike-stewart added a commit to mike-stewart/rails that referenced this pull request Jun 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants