Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise an exception when model without primary key calls .find_with_ids #12549

Merged
merged 1 commit into from Oct 21, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions 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*
Expand Down
2 changes: 2 additions & 0 deletions activerecord/lib/active_record/relation/finder_methods.rb
Expand Up @@ -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?

Expand Down
7 changes: 7 additions & 0 deletions activerecord/test/cases/finder_test.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down