aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYves Senn <yves.senn@gmail.com>2013-10-21 01:18:41 -0700
committerYves Senn <yves.senn@gmail.com>2013-10-21 01:18:41 -0700
commit73641563917e3a69b63a1b11c8ebe33c469e8951 (patch)
tree9aa72fba480bafb4694e864abaf0a9350ba4a5e8
parent6205476a53865cec15895dc0546a5ececace5b7a (diff)
parente2419a451aaeafc166b8677540bc29cd8b0c97fd (diff)
downloadrails-73641563917e3a69b63a1b11c8ebe33c469e8951.tar.gz
rails-73641563917e3a69b63a1b11c8ebe33c469e8951.tar.bz2
rails-73641563917e3a69b63a1b11c8ebe33c469e8951.zip
Merge pull request #12549 from makimoto/raise-when-find-without-pk
Raise an exception when model without primary key calls .find_with_ids
-rw-r--r--activerecord/CHANGELOG.md4
-rw-r--r--activerecord/lib/active_record/relation/finder_methods.rb2
-rw-r--r--activerecord/test/cases/finder_test.rb7
3 files changed, 13 insertions, 0 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 22a37810bc..e17394002d 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,7 @@
+* Raise an exception when model without primary key calls `.find_with_ids`.
+
+ *Shimpei Makimoto*
+
* Make `Relation#empty?` use `exists?` instead of `count`.
*Szymon Nowak*
diff --git a/activerecord/lib/active_record/relation/finder_methods.rb b/activerecord/lib/active_record/relation/finder_methods.rb
index fe75a32545..ae9717e783 100644
--- a/activerecord/lib/active_record/relation/finder_methods.rb
+++ b/activerecord/lib/active_record/relation/finder_methods.rb
@@ -297,6 +297,8 @@ module ActiveRecord
protected
def find_with_ids(*ids)
+ raise UnknownPrimaryKey.new(@klass) if primary_key.nil?
+
expects_array = ids.first.kind_of?(Array)
return ids.first if expects_array && ids.first.empty?
diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb
index 1b9ef14ec9..8c1974c77b 100644
--- a/activerecord/test/cases/finder_test.rb
+++ b/activerecord/test/cases/finder_test.rb
@@ -11,6 +11,7 @@ require 'models/project'
require 'models/developer'
require 'models/customer'
require 'models/toy'
+require 'models/matey'
class FinderTest < ActiveRecord::TestCase
fixtures :companies, :topics, :entrants, :developers, :developers_projects, :posts, :comments, :accounts, :authors, :customers, :categories, :categorizations
@@ -860,6 +861,12 @@ class FinderTest < ActiveRecord::TestCase
Toy.reset_primary_key
end
+ def test_find_without_primary_key
+ assert_raises(ActiveRecord::UnknownPrimaryKey) do
+ Matey.find(1)
+ end
+ end
+
def test_finder_with_offset_string
assert_nothing_raised(ActiveRecord::StatementInvalid) { Topic.all.merge!(:offset => "3").to_a }
end