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

Conversation

simi
Copy link
Contributor

@simi simi commented Jan 16, 2020

Hello, I have spotted when cursor is used on association the association is actually loaded (sql query is triggered).

I have found out it is related to type check in iterate_type. Using == to compare type triggers the association query. I have added type check of ŧype.class to avoid calling equality operator on active record relation.

There's need to access @type via instance_variable_get on cursor in specs, but I think it is not worth it to expose type to public just because of specs.

before (cursor is initialized and prices relation is loaded)

[4] pry(#<TestPostgresqlCursor>)> cursor = Product.first.prices.each_instance;nil
D, [2020-01-17T00:14:47.381230 #1955109] DEBUG -- :   Product Load (0.7ms)  SELECT "products".* FROM "products" ORDER BY "products"."id" ASC LIMIT $1  [["LIMIT", 1]]
D, [2020-01-17T00:14:47.382515 #1955109] DEBUG -- :   Price Load (0.3ms)  SELECT "prices".* FROM "prices" WHERE "prices"."product_id" = $1  [["product_id", 1]]
=> nil

after (cursor is initialized and prices relation is not loaded)

[2] pry(#<TestPostgresqlCursor>)> cursor = Product.first.prices.each_instance;nil                                                                                                             
D, [2020-01-17T00:13:48.292153 #1954698] DEBUG -- :   Product Load (0.3ms)  SELECT "products".* FROM "products" ORDER BY "products"."id" ASC LIMIT $1  [["LIMIT", 1]]
=> nil                                                                                                                                                                                        

@afair afair merged commit 1362ec7 into afair:master Jan 17, 2020
@simi simi deleted the do-not-load-association branch January 17, 2020 20:18
@simi simi restored the do-not-load-association branch January 20, 2020 10:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants