Skip to content

Do not load association during initialization of cursor. #51

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

Merged
merged 1 commit into from
Jan 17, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ desc "Setup testing database and table"
task :setup do
sh %q(createdb postgresql_cursor_test)
sh %Q<echo "create table products ( id serial primary key, data varchar);" | psql postgresql_cursor_test>
sh %Q<echo "create table prices ( id serial primary key, data varchar, product_id integer);" | psql postgresql_cursor_test>
end
9 changes: 6 additions & 3 deletions lib/postgresql_cursor/cursor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,14 @@ def initialize(sql, options={})
@batched = false
end

# Specify the type to instantiate, or reset to return a Hash
# Specify the type to instantiate, or reset to return a Hash.
#
# Explicitly check for type class to prevent calling equality
# operator on active record relation, which will load it.
def iterate_type(type=nil)
if type.nil? || type == Hash
if type.nil? || (type.class == Class && type == Hash)
@iterate = :each_row
elsif type == Array
elsif type.class == Class && type == Array
@iterate = :each_array
else
@iterate = :each_instance
Expand Down
6 changes: 6 additions & 0 deletions test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
username: ENV['TEST_USER'] || ENV['USER'] || 'postgresql_cursor')

class Product < ActiveRecord::Base
has_many :prices

# create table records (id serial primary key);
def self.generate(max=1_000)
max.times do |i|
Expand All @@ -18,5 +20,9 @@ def self.generate(max=1_000)
end
end

class Price < ActiveRecord::Base
belongs_to :product
end

Product.destroy_all
Product.generate(1000)
5 changes: 5 additions & 0 deletions test/test_postgresql_cursor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,9 @@ def test_bad_sql
assert_match(/bad_table/, e.message)
end
end

def test_relation_association_is_not_loaded
cursor = Product.first.prices.each_instance
refute cursor.instance_variable_get(:@type).loaded?
end
end