Skip to content

Commit

Permalink
Use correct foreign key of belongs_to associations
Browse files Browse the repository at this point in the history
  • Loading branch information
foobear committed Dec 3, 2013
1 parent cc5ab30 commit d415f75
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 5 deletions.
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/assignable_values/version.rb
@@ -1,3 +1,3 @@
module AssignableValues
VERSION = '0.8.0'
VERSION = '0.8.1'
end
2 changes: 1 addition & 1 deletion 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
Expand Down
2 changes: 1 addition & 1 deletion 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
Expand Down
2 changes: 1 addition & 1 deletion 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
Expand Down
12 changes: 12 additions & 0 deletions spec/shared/assignable_values/active_record_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit d415f75

Please sign in to comment.