diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2018-08-29 16:00:41 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-08-29 16:00:41 -0500 |
commit | ed1eda271c7ac82ecb7bd94b6fa1b0093e648a3e (patch) | |
tree | 2423c670f8314d1ca6bae2e58077c7cdbc42056e /activerecord/lib | |
parent | 3d2caab7dc92a13d4dd369678d5b4ce659df8e52 (diff) | |
parent | 1c7275a0ba05aaa27e5999eaa10b698d890fa157 (diff) | |
download | rails-ed1eda271c7ac82ecb7bd94b6fa1b0093e648a3e.tar.gz rails-ed1eda271c7ac82ecb7bd94b6fa1b0093e648a3e.tar.bz2 rails-ed1eda271c7ac82ecb7bd94b6fa1b0093e648a3e.zip |
Merge pull request #33729 from kddeisz/plural-automatic-inverse
Find inverse associations with plural names
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/reflection.rb | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index 6d2f75a3ae..b2110f727c 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -612,9 +612,21 @@ module ActiveRecord # returns either +nil+ or the inverse association name that it finds. def automatic_inverse_of - if can_find_inverse_of_automatically?(self) - inverse_name = ActiveSupport::Inflector.underscore(options[:as] || active_record.name.demodulize).to_sym + return unless can_find_inverse_of_automatically?(self) + inverse_name_candidates = + if options[:as] + [options[:as]] + else + active_record_name = active_record.name.demodulize + [active_record_name, ActiveSupport::Inflector.pluralize(active_record_name)] + end + + inverse_name_candidates.map! do |candidate| + ActiveSupport::Inflector.underscore(candidate).to_sym + end + + inverse_name_candidates.detect do |inverse_name| begin reflection = klass._reflect_on_association(inverse_name) rescue NameError @@ -623,9 +635,7 @@ module ActiveRecord reflection = false end - if valid_inverse_reflection?(reflection) - return inverse_name - end + valid_inverse_reflection?(reflection) end end |