diff options
author | Carlos Antonio da Silva <carlosantoniodasilva@gmail.com> | 2013-04-04 22:02:36 -0300 |
---|---|---|
committer | Carlos Antonio da Silva <carlosantoniodasilva@gmail.com> | 2013-04-04 22:09:40 -0300 |
commit | 034f254d59dfa1c1f508db574fac5df263336ba1 (patch) | |
tree | 82afa49edd7ff6b849d1fa8ff178244275bab9c9 /activerecord/lib/active_record | |
parent | 797fcdf738a2a2772544731027d4fc5ca9d358bc (diff) | |
download | rails-034f254d59dfa1c1f508db574fac5df263336ba1.tar.gz rails-034f254d59dfa1c1f508db574fac5df263336ba1.tar.bz2 rails-034f254d59dfa1c1f508db574fac5df263336ba1.zip |
Use the correct pk field from the reflected class to find the old record
The implementation was using the source class foreign key field instead
of the reflected primary key one to find the old record.
For instance, for this scenario
class Bulb < ActiveRecord::Base
belongs_to :car, :touch => true
end
class Car < ActiveRecord::Base
has_many :bulbs
end
the current implementation was trying to do this query:
Car.where(car_id: X).first
where we should be doing this query:
Car.where(id: X).first
This should hopefully fix the build.
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r-- | activerecord/lib/active_record/associations/builder/belongs_to.rb | 3 |
1 files changed, 2 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/associations/builder/belongs_to.rb b/activerecord/lib/active_record/associations/builder/belongs_to.rb index da7e8cda3a..e0ec4392b4 100644 --- a/activerecord/lib/active_record/associations/builder/belongs_to.rb +++ b/activerecord/lib/active_record/associations/builder/belongs_to.rb @@ -71,8 +71,9 @@ module ActiveRecord::Associations::Builder foreign_key_field = #{reflection.foreign_key.inspect} if changed_attributes.key?(foreign_key_field) reflection_klass = #{reflection.klass} + primary_key_field = reflection_klass.primary_key old_foreign_id = changed_attributes[foreign_key_field] - old_record = reflection_klass.where(foreign_key_field => old_foreign_id).first + old_record = reflection_klass.where(primary_key_field => old_foreign_id).first if old_record old_record.touch #{options[:touch].inspect if options[:touch] != true} end |