Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Raise an exception when model without primary key calls .find_with_ids

  • Loading branch information...
commit e2419a451aaeafc166b8677540bc29cd8b0c97fd 1 parent 6205476
@makimoto makimoto authored
View
4 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*
View
2  activerecord/lib/active_record/relation/finder_methods.rb
@@ -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?
View
7 activerecord/test/cases/finder_test.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.