mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #12549 from makimoto/raise-when-find-without-pk
Raise an exception when model without primary key calls .find_with_ids
This commit is contained in:
commit
7364156391
3 changed files with 13 additions and 0 deletions
|
@ -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`.
|
* Make `Relation#empty?` use `exists?` instead of `count`.
|
||||||
|
|
||||||
*Szymon Nowak*
|
*Szymon Nowak*
|
||||||
|
|
|
@ -297,6 +297,8 @@ module ActiveRecord
|
||||||
protected
|
protected
|
||||||
|
|
||||||
def find_with_ids(*ids)
|
def find_with_ids(*ids)
|
||||||
|
raise UnknownPrimaryKey.new(@klass) if primary_key.nil?
|
||||||
|
|
||||||
expects_array = ids.first.kind_of?(Array)
|
expects_array = ids.first.kind_of?(Array)
|
||||||
return ids.first if expects_array && ids.first.empty?
|
return ids.first if expects_array && ids.first.empty?
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,7 @@ require 'models/project'
|
||||||
require 'models/developer'
|
require 'models/developer'
|
||||||
require 'models/customer'
|
require 'models/customer'
|
||||||
require 'models/toy'
|
require 'models/toy'
|
||||||
|
require 'models/matey'
|
||||||
|
|
||||||
class FinderTest < ActiveRecord::TestCase
|
class FinderTest < ActiveRecord::TestCase
|
||||||
fixtures :companies, :topics, :entrants, :developers, :developers_projects, :posts, :comments, :accounts, :authors, :customers, :categories, :categorizations
|
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
|
Toy.reset_primary_key
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_find_without_primary_key
|
||||||
|
assert_raises(ActiveRecord::UnknownPrimaryKey) do
|
||||||
|
Matey.find(1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def test_finder_with_offset_string
|
def test_finder_with_offset_string
|
||||||
assert_nothing_raised(ActiveRecord::StatementInvalid) { Topic.all.merge!(:offset => "3").to_a }
|
assert_nothing_raised(ActiveRecord::StatementInvalid) { Topic.all.merge!(:offset => "3").to_a }
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue