Permalink
Browse files

Merge pull request #12549 from makimoto/raise-when-find-without-pk

Raise an exception when model without primary key calls .find_with_ids
  • Loading branch information...
2 parents 6205476 + e2419a4 commit 73641563917e3a69b63a1b11c8ebe33c469e8951 @senny senny committed Oct 21, 2013
@@ -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*
@@ -297,6 +297,8 @@ def using_limitable_reflections?(reflections)
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?
@@ -11,6 +11,7 @@
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 @@ def test_find_one_message_with_custom_primary_key
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

0 comments on commit 7364156

Please sign in to comment.