Skip to content

Commit b370459

Browse files
committed
Do not load association during initialization of cursor.
1 parent b2c9e4e commit b370459

File tree

4 files changed

+18
-3
lines changed

4 files changed

+18
-3
lines changed

Rakefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ desc "Setup testing database and table"
2121
task :setup do
2222
sh %q(createdb postgresql_cursor_test)
2323
sh %Q<echo "create table products ( id serial primary key, data varchar);" | psql postgresql_cursor_test>
24+
sh %Q<echo "create table prices ( id serial primary key, data varchar, product_id integer);" | psql postgresql_cursor_test>
2425
end

lib/postgresql_cursor/cursor.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,14 @@ def initialize(sql, options={})
5050
@batched = false
5151
end
5252

53-
# Specify the type to instantiate, or reset to return a Hash
53+
# Specify the type to instantiate, or reset to return a Hash.
54+
#
55+
# Explicitly check for type class to prevent calling equality
56+
# operator on active record relation, which will load it.
5457
def iterate_type(type=nil)
55-
if type.nil? || type == Hash
58+
if type.nil? || (type.class == Class && type == Hash)
5659
@iterate = :each_row
57-
elsif type == Array
60+
elsif type.class == Class && type == Array
5861
@iterate = :each_array
5962
else
6063
@iterate = :each_instance

test/helper.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
username: ENV['TEST_USER'] || ENV['USER'] || 'postgresql_cursor')
1111

1212
class Product < ActiveRecord::Base
13+
has_many :prices
14+
1315
# create table records (id serial primary key);
1416
def self.generate(max=1_000)
1517
max.times do |i|
@@ -18,5 +20,9 @@ def self.generate(max=1_000)
1820
end
1921
end
2022

23+
class Price < ActiveRecord::Base
24+
belongs_to :product
25+
end
26+
2127
Product.destroy_all
2228
Product.generate(1000)

test/test_postgresql_cursor.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,4 +191,9 @@ def test_bad_sql
191191
assert_match(/bad_table/, e.message)
192192
end
193193
end
194+
195+
def test_relation_association_is_not_loaded
196+
cursor = Product.first.prices.each_instance
197+
refute cursor.instance_variable_get(:@type).loaded?
198+
end
194199
end

0 commit comments

Comments
 (0)