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

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
2 participants
Contributor

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
fdf4eae.

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
first(n)/last(n).

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.

Conflicts:
	activerecord/CHANGELOG.md
	activerecord/lib/active_record/associations/collection_association.rb
694334c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment