From 01ec38a1ca2114a7ef819986304df4060571bed3 Mon Sep 17 00:00:00 2001 From: Yuki Nishijima Date: Fri, 13 Jan 2017 04:26:43 +0000 Subject: [PATCH] Fixed a bug where the #total_count raises an exception fixes #849 --- kaminari-core/lib/kaminari/models/page_scope_methods.rb | 2 +- .../active_record/active_record_relation_methods_test.rb | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/kaminari-core/lib/kaminari/models/page_scope_methods.rb b/kaminari-core/lib/kaminari/models/page_scope_methods.rb index 1c2cce3..bca103d 100644 --- a/kaminari-core/lib/kaminari/models/page_scope_methods.rb +++ b/kaminari-core/lib/kaminari/models/page_scope_methods.rb @@ -5,7 +5,7 @@ module Kaminari # Model.page(3).per(10) def per(num, max_per_page: nil) max_per_page ||= ((defined?(@_max_per_page) && @_max_per_page) || self.max_per_page) - @_per = num || default_per_page + @_per = (num || default_per_page).to_i if (n = num.to_i) < 0 || !(/^\d/ =~ num.to_s) self elsif n.zero? diff --git a/kaminari-core/test/models/active_record/active_record_relation_methods_test.rb b/kaminari-core/test/models/active_record/active_record_relation_methods_test.rb index b17a8a9..e9b8fc2 100644 --- a/kaminari-core/test/models/active_record/active_record_relation_methods_test.rb +++ b/kaminari-core/test/models/active_record/active_record_relation_methods_test.rb @@ -84,6 +84,10 @@ if defined? ActiveRecord test "throw an exception when calculating total_count when the query includes column aliases used by a group-by clause" do assert_equal 3, Book.joins(authorships: :user).select("users.name as author_name").group('users.name').page(1).total_count end + + test 'total_count is calculable with page 1 per "5" (the string)' do + assert_equal 7, User.page(1).per('5').load.total_count + end end end end