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

Find inverse associations with plural names #33729

Merged
merged 1 commit into from Aug 29, 2018

Conversation

Projects
None yet
5 participants
@kddeisz
Contributor

kddeisz commented Aug 27, 2018

Previously ActiveRecord couldn't find inverse associations if they were plural, which is a pretty standard use case. This commit changes the behavior to first attempt to find the singular version, then attempt to find the plural version. That makes it work and find plural associations as in the example below:

class Post
  has_many :comments
end

class Comment
  belongs_to :post
end

Previously the :post association reflection would only attempt to find a comment inverse, as opposed to both a comment and comments inverse. This drastically cuts down on the number of associations that don't have inverses, and can end up fixing a lot of strange behavior where the parent doesn't save the child records.

@rails-bot

This comment has been minimized.

Show comment
Hide comment
@rails-bot

rails-bot Aug 27, 2018

r? @schneems

(@rails-bot has picked a reviewer for you, use r? to override)

rails-bot commented Aug 27, 2018

r? @schneems

(@rails-bot has picked a reviewer for you, use r? to override)

Show outdated Hide outdated activerecord/lib/active_record/reflection.rb
Find inverse associations with plural names
Previously ActiveRecord couldn't find inverse associations if they were plural, which is a pretty standard use case. This commit changes the behavior to first attempt to find the singular version, then attempt to find the plural version. That makes it work and find plural associations as in the example below:

```
class Post
  has_many :comments
end

class Comment
  belongs_to :post
end
```

Previously the `:post` association reflection would only attempt to find a `comment` inverse, as opposed to both a `comment` and `comments` inverse.
@kddeisz

This comment has been minimized.

Show comment
Hide comment
@kddeisz

kddeisz Aug 29, 2018

Contributor

@rafaelfranca any other feedback on this one?

Contributor

kddeisz commented Aug 29, 2018

@rafaelfranca any other feedback on this one?

@rafaelfranca rafaelfranca merged commit ed1eda2 into rails:master Aug 29, 2018

2 checks passed

codeclimate All good!
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

@kddeisz kddeisz deleted the kddeisz:plural-automatic-inverse branch Aug 29, 2018

@yahonda

This comment has been minimized.

Show comment
Hide comment
@yahonda

yahonda Aug 29, 2018

Contributor

Let me inform this test fails with Ruby 2.5.1 and ruby-head. Working fine with 2.4.4.

CI shows the same failure. https://travis-ci.org/rails/rails/jobs/422266836

$ ruby -v
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
$ for i in sqlite3 postgresql mysql2 ; do ARCONN=$i bundle exec ruby -w -Itest test/cases/associations/inverse_associations_test.rb -n test_finds_inverse_of_for_plural_associations;  done
Using sqlite3
Run options: -n test_finds_inverse_of_for_plural_associations --seed 49610

# Running:

E

Error:
InversePolymorphicBelongsToTests#test_finds_inverse_of_for_plural_associations:
NoMethodError: undefined method `name' for nil:NilClass
    test/cases/associations/inverse_associations_test.rb:735:in `test_finds_inverse_of_for_plural_associations'


rails test test/cases/associations/inverse_associations_test.rb:734



Finished in 0.026777s, 37.3462 runs/s, 0.0000 assertions/s.
1 runs, 0 assertions, 0 failures, 1 errors, 0 skips
Using postgresql
Run options: -n test_finds_inverse_of_for_plural_associations --seed 42043

# Running:

E

Error:
InversePolymorphicBelongsToTests#test_finds_inverse_of_for_plural_associations:
NoMethodError: undefined method `name' for nil:NilClass
    test/cases/associations/inverse_associations_test.rb:735:in `test_finds_inverse_of_for_plural_associations'


rails test test/cases/associations/inverse_associations_test.rb:734



Finished in 0.088667s, 11.2782 runs/s, 0.0000 assertions/s.
1 runs, 0 assertions, 0 failures, 1 errors, 0 skips
Using mysql2
Run options: -n test_finds_inverse_of_for_plural_associations --seed 15896

# Running:

E

Error:
InversePolymorphicBelongsToTests#test_finds_inverse_of_for_plural_associations:
NoMethodError: undefined method `name' for nil:NilClass
    test/cases/associations/inverse_associations_test.rb:735:in `test_finds_inverse_of_for_plural_associations'


rails test test/cases/associations/inverse_associations_test.rb:734



Finished in 0.073057s, 13.6879 runs/s, 0.0000 assertions/s.
1 runs, 0 assertions, 0 failures, 1 errors, 0 skips
$
Contributor

yahonda commented Aug 29, 2018

Let me inform this test fails with Ruby 2.5.1 and ruby-head. Working fine with 2.4.4.

CI shows the same failure. https://travis-ci.org/rails/rails/jobs/422266836

$ ruby -v
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
$ for i in sqlite3 postgresql mysql2 ; do ARCONN=$i bundle exec ruby -w -Itest test/cases/associations/inverse_associations_test.rb -n test_finds_inverse_of_for_plural_associations;  done
Using sqlite3
Run options: -n test_finds_inverse_of_for_plural_associations --seed 49610

# Running:

E

Error:
InversePolymorphicBelongsToTests#test_finds_inverse_of_for_plural_associations:
NoMethodError: undefined method `name' for nil:NilClass
    test/cases/associations/inverse_associations_test.rb:735:in `test_finds_inverse_of_for_plural_associations'


rails test test/cases/associations/inverse_associations_test.rb:734



Finished in 0.026777s, 37.3462 runs/s, 0.0000 assertions/s.
1 runs, 0 assertions, 0 failures, 1 errors, 0 skips
Using postgresql
Run options: -n test_finds_inverse_of_for_plural_associations --seed 42043

# Running:

E

Error:
InversePolymorphicBelongsToTests#test_finds_inverse_of_for_plural_associations:
NoMethodError: undefined method `name' for nil:NilClass
    test/cases/associations/inverse_associations_test.rb:735:in `test_finds_inverse_of_for_plural_associations'


rails test test/cases/associations/inverse_associations_test.rb:734



Finished in 0.088667s, 11.2782 runs/s, 0.0000 assertions/s.
1 runs, 0 assertions, 0 failures, 1 errors, 0 skips
Using mysql2
Run options: -n test_finds_inverse_of_for_plural_associations --seed 15896

# Running:

E

Error:
InversePolymorphicBelongsToTests#test_finds_inverse_of_for_plural_associations:
NoMethodError: undefined method `name' for nil:NilClass
    test/cases/associations/inverse_associations_test.rb:735:in `test_finds_inverse_of_for_plural_associations'


rails test test/cases/associations/inverse_associations_test.rb:734



Finished in 0.073057s, 13.6879 runs/s, 0.0000 assertions/s.
1 runs, 0 assertions, 0 failures, 1 errors, 0 skips
$
@yahonda

This comment has been minimized.

Show comment
Hide comment
@yahonda

yahonda Aug 29, 2018

Contributor

Addressed by fa132ef

Contributor

yahonda commented Aug 29, 2018

Addressed by fa132ef

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