From d415f75460bfdcd05acc271684fb309db0c1b81f Mon Sep 17 00:00:00 2001 From: Arne Hartherz Date: Tue, 3 Dec 2013 08:58:58 +0100 Subject: [PATCH] Use correct foreign key of belongs_to associations --- .../restriction/belongs_to_association.rb | 7 ++++++- lib/assignable_values/version.rb | 2 +- spec/rails-2.3/Gemfile.lock | 2 +- spec/rails-3.0/Gemfile.lock | 2 +- spec/rails-3.2/Gemfile.lock | 2 +- spec/shared/assignable_values/active_record_spec.rb | 12 ++++++++++++ 6 files changed, 22 insertions(+), 5 deletions(-) diff --git a/lib/assignable_values/active_record/restriction/belongs_to_association.rb b/lib/assignable_values/active_record/restriction/belongs_to_association.rb index 3a9a828..5b6712e 100644 --- a/lib/assignable_values/active_record/restriction/belongs_to_association.rb +++ b/lib/assignable_values/active_record/restriction/belongs_to_association.rb @@ -10,7 +10,12 @@ def association_class end def association_id_method - "#{property}_id" + association = model.reflect_on_association(property) + if association.respond_to?(:foreign_key) + association.foreign_key # Rails >= 3.1 + else + association.primary_key_name # Rails 2 + 3.0 + end end def error_property diff --git a/lib/assignable_values/version.rb b/lib/assignable_values/version.rb index 07d79ff..319dd39 100644 --- a/lib/assignable_values/version.rb +++ b/lib/assignable_values/version.rb @@ -1,3 +1,3 @@ module AssignableValues - VERSION = '0.8.0' + VERSION = '0.8.1' end diff --git a/spec/rails-2.3/Gemfile.lock b/spec/rails-2.3/Gemfile.lock index b835144..0fadeeb 100644 --- a/spec/rails-2.3/Gemfile.lock +++ b/spec/rails-2.3/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: ../.. specs: - assignable_values (0.7.2) + assignable_values (0.8.1) activerecord GEM diff --git a/spec/rails-3.0/Gemfile.lock b/spec/rails-3.0/Gemfile.lock index f6b143b..8391bdd 100644 --- a/spec/rails-3.0/Gemfile.lock +++ b/spec/rails-3.0/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: ../.. specs: - assignable_values (0.7.2) + assignable_values (0.8.1) activerecord GEM diff --git a/spec/rails-3.2/Gemfile.lock b/spec/rails-3.2/Gemfile.lock index 106c487..864af45 100644 --- a/spec/rails-3.2/Gemfile.lock +++ b/spec/rails-3.2/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: ../.. specs: - assignable_values (0.7.2) + assignable_values (0.8.1) activerecord GEM diff --git a/spec/shared/assignable_values/active_record_spec.rb b/spec/shared/assignable_values/active_record_spec.rb index b82d8f0..283268f 100644 --- a/spec/shared/assignable_values/active_record_spec.rb +++ b/spec/shared/assignable_values/active_record_spec.rb @@ -186,6 +186,18 @@ error.should == I18n.t('errors.messages.inclusion') end + it 'uses the defined foreign key of the association' do + klass = Song.disposable_copy do + belongs_to :creator, :foreign_key => 'artist_id', :class_name => 'Artist' + + assignable_values_for :creator do + [] + end + end + + klass.new(:creator => Artist.new).should have(1).error_on(:artist_id) + end + it 'should allow a nil association if the :allow_blank option is set' do klass = Song.disposable_copy do assignable_values_for :artist, :allow_blank => true do