set inverses correctly for first(n) and last(n) #8094

wants to merge 1 commit into


None yet
2 participants

al2o3cr commented Nov 1, 2012

The code introduced in #7377 assumes that first and last always return a single record. This is incorrect for calling first with an integer argument.

The attached commit (against 3-2-stable) handles this case correctly and includes a test.

I'm not super-happy with the resulting code; there's way too much is_a? checking going on in first_or_last, but this will work for now to get 3.2.9 out.

@carlosantoniodasilva carlosantoniodasilva added a commit that referenced this pull request Nov 1, 2012

@carlosantoniodasilva carlosantoniodasilva Fix issue with collection associations and first(n)/last(n)
When calling first(n) or last(n) in a collection, Active Record was
improperly trying to set the inverse of instance in case that option
existed. This change was introduced by

In such cases we don't need to do that "manually", since the way
collection will be loaded will already handle that, so we just skip
setting the inverse association when any argument is given to

The test included ensures that these scenarios will have the inverse of
instance set properly.

Fixes #8087, Closes #8094.

Squashed cherry-pick from d37d40b and c368b66.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment