aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/attribute_methods/read.rb
diff options
context:
space:
mode:
authorJon Leighton <j@jonathanleighton.com>2011-10-05 01:09:43 +0100
committerJon Leighton <j@jonathanleighton.com>2011-10-05 01:11:40 +0100
commitee2be435b1e5c0e94a4ee93a1a310e0471a77d07 (patch)
treed30036d8c7f8520df4c6cd4c47e0f7733abe3525 /activerecord/lib/active_record/attribute_methods/read.rb
parent5711a35ad8faa3fb6d138b234cbe9acfad27a9a8 (diff)
downloadrails-ee2be435b1e5c0e94a4ee93a1a310e0471a77d07.tar.gz
rails-ee2be435b1e5c0e94a4ee93a1a310e0471a77d07.tar.bz2
rails-ee2be435b1e5c0e94a4ee93a1a310e0471a77d07.zip
Raise error on unknown primary key.
If we don't have a primary key when we ask for it, it's better to fail fast. Fixes GH #2307.
Diffstat (limited to 'activerecord/lib/active_record/attribute_methods/read.rb')
-rw-r--r--activerecord/lib/active_record/attribute_methods/read.rb6
1 files changed, 3 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/attribute_methods/read.rb b/activerecord/lib/active_record/attribute_methods/read.rb
index 4174e4da09..8566ecad14 100644
--- a/activerecord/lib/active_record/attribute_methods/read.rb
+++ b/activerecord/lib/active_record/attribute_methods/read.rb
@@ -40,7 +40,7 @@ module ActiveRecord
define_read_method(attr_name, attr_name, columns_hash[attr_name])
end
- if attr_name == primary_key && attr_name != "id"
+ if primary_key? && attr_name == primary_key && attr_name != "id"
define_read_method('id', attr_name, columns_hash[attr_name])
end
end
@@ -63,7 +63,7 @@ module ActiveRecord
cast_code = column.type_cast_code('v')
access_code = "(v=@attributes['#{attr_name}']) && #{cast_code}"
- unless attr_name.to_s == self.primary_key.to_s
+ unless primary_key? && attr_name.to_s == primary_key.to_s
access_code.insert(0, "missing_attribute('#{attr_name}', caller) unless @attributes.has_key?('#{attr_name}'); ")
end
@@ -107,7 +107,7 @@ module ActiveRecord
def _read_attribute(attr_name)
attr_name = attr_name.to_s
- attr_name = self.class.primary_key if attr_name == 'id'
+ attr_name = self.class.primary_key? && self.class.primary_key if attr_name == 'id'
value = @attributes[attr_name]
unless value.nil?
if column = column_for_attribute(attr_name)