rails_select_on_includes icon indicating copy to clipboard operation
rails_select_on_includes copied to clipboard

Support for Rails 6.1.1

Open lremes opened this issue 4 years ago • 7 comments

When running gem againts Rails 6.1.1, I get the following error and stack trace:

ArgumentError at XXX
wrong number of arguments (given 5, expected 6)

construct
activerecord (6.1.1) lib/active_record/associations/join_dependency.rb, line 238
block (2 levels) in instantiate
rails_select_on_includes (6.0.1) lib/rails_select_on_includes.rb, line 104
block in each
activerecord (6.1.1) lib/active_record/result.rb, line 62
each
activerecord (6.1.1) lib/active_record/result.rb, line 62
each
activerecord (6.1.1) lib/active_record/result.rb, line 62
block in instantiate
rails_select_on_includes (6.0.1) lib/rails_select_on_includes.rb, line 96
instrument
activesupport (6.1.1) lib/active_support/notifications/instrumenter.rb, line 24
instantiate
rails_select_on_includes (6.0.1) lib/rails_select_on_includes.rb, line 95
block (2 levels) in exec_queries
rails_select_on_includes (6.0.1) lib/rails_select_on_includes.rb, line 130
apply_join_dependency
activerecord (6.1.1) lib/active_record/relation/finder_methods.rb, line 421
block in exec_queries
rails_select_on_includes (6.0.1) lib/rails_select_on_includes.rb, line 120
skip_query_cache_if_necessary
activerecord (6.1.1) lib/active_record/relation.rb, line 868
exec_queries
rails_select_on_includes (6.0.1) lib/rails_select_on_includes.rb, line 117
load
activerecord (6.1.1) lib/active_record/relation.rb, line 638
records
activerecord (6.1.1) lib/active_record/relation.rb, line 249
to_ary
activerecord (6.1.1) lib/active_record/relation.rb, line 244

The line is:

 def construct(ar_parent, parent, row, seen, model_cache, strict_loading_value)
          return if ar_parent.nil?

lremes avatar Jan 13 '21 13:01 lremes

Do we actually need support for the new Rails? I was under impression that finally they did fix the bug. Can you check on your project?

alekseyl avatar Jan 13 '21 14:01 alekseyl

the original bug is finally closed: https://github.com/rails/rails/issues/15185

alekseyl avatar Jan 13 '21 14:01 alekseyl

I tried removing the gem from Gemfile, but it did not work out-of-the-box. I get an error about missing attribute.

lremes avatar Jan 13 '21 14:01 lremes

hm, can you then re-open original rails bugs? at least one of them.

I'll try to take a look and fix it by the end of the week.

alekseyl avatar Jan 13 '21 14:01 alekseyl

Just to note, this is what the old version with the gem generates:

 SELECT COALESCE(c.task_attachment_count, 0) AS task_attachment_count, "tasks"."id" AS t0_r0, "tasks"."board_id" AS t0_r1, "tasks"."account_id" AS t0_r2, "tasks"."responsible_user_id" AS t0_r3, "tasks"."data" AS t0_r4, "tasks"."finding_id" AS t0_r5, "tasks"."created_at" AS t0_r6, "tasks"."updated_at" AS t0_r7, "tasks"."ui_position" AS t0_r8, "tasks"."status" AS t0_r9, "tasks"."assessment_id" AS t0_r10, "tasks"."archived" AS t0_r11, "tasks"."stakeholder_id" AS t0_r12, "tasks"."chemical_id" AS t0_r13, "tasks"."maintenance_register_entry_id" AS t0_r14, "tasks"."permit_register_entry_id" AS t0_r15, "tasks"."registers_obligation_id" AS t0_r16, "tasks"."registers_data_processing_operation_id" AS t0_r17, "tasks"."mandatory_document_id" AS t0_r18, "tasks"."accounts_requirement_id" AS t0_r19, "tasks"."uuid" AS t0_r20, "users"."id" AS t1_r0, "users"."email" AS t1_r1, "users"."crypted_password" AS t1_r2, "users"."password_salt" AS t1_r3, "users"."persistence_token" AS t1_r4, "users"."login_count" AS t1_r5, "users"."failed_login_count" AS t1_r6, "users"."last_request_at" AS t1_r7, "users"."last_login_at" AS t1_r8, "users"."current_login_at" AS t1_r9, "users"."last_login_ip" AS t1_r10, "users"."current_login_ip" AS t1_r11, "users"."first_name" AS t1_r12, "users"."last_name" AS t1_r13, "users"."middle_name" AS t1_r14, "users"."active" AS t1_r15, "users"."phone" AS t1_r16, "users"."account_id" AS t1_r17, "users"."created_at" AS t1_r18, "users"."updated_at" AS t1_r19, "users"."locale" AS t1_r20, "users"."perishable_token" AS t1_r21, "users"."allowed_ips" AS t1_r22, "users"."ip_authentication" AS t1_r23, "users"."single_access_token" AS t1_r24, "users"."omniauth_data" AS t1_r25, "users"."memo" AS t1_r26, "users"."access_token" AS t1_r27, "users"."end_user_agreement_accepted_at" AS t1_r28, "users"."privacy_policy_accepted" AS t1_r29, "users"."data" AS t1_r30, "users"."marketing_consent" AS t1_r31, "users"."approved" AS t1_r32, "users"."confirmed" AS t1_r33, "users"."uuid" AS t1_r34 FROM "tasks" LEFT OUTER JOIN "users" ON "users"."id" = "tasks"."responsible_user_id" LEFT OUTER JOIN (
			 SELECT d.task_id, COUNT(*) task_attachment_count
			 FROM   attachments d
			 GROUP BY d.task_id
		   ) c ON c.task_id = tasks.id WHERE ((tasks.archived = FALSE)) AND "tasks"."account_id" = $1  [["account_id", 121]]

And rails 6.1.1 without the gem generates the following:


SELECT COALESCE(c.task_attachment_count, 0) AS task_attachment_count, "tasks"."id" AS t0_r0, "users"."id" AS t1_r0, "users"."email" AS t1_r1, "users"."crypted_password" AS t1_r2, "users"."password_salt" AS t1_r3, "users"."persistence_token" AS t1_r4, "users"."login_count" AS t1_r5, "users"."failed_login_count" AS t1_r6, "users"."last_request_at" AS t1_r7, "users"."last_login_at" AS t1_r8, "users"."current_login_at" AS t1_r9, "users"."last_login_ip" AS t1_r10, "users"."current_login_ip" AS t1_r11, "users"."first_name" AS t1_r12, "users"."last_name" AS t1_r13, "users"."middle_name" AS t1_r14, "users"."active" AS t1_r15, "users"."phone" AS t1_r16, "users"."account_id" AS t1_r17, "users"."created_at" AS t1_r18, "users"."updated_at" AS t1_r19, "users"."locale" AS t1_r20, "users"."perishable_token" AS t1_r21, "users"."allowed_ips" AS t1_r22, "users"."ip_authentication" AS t1_r23, "users"."single_access_token" AS t1_r24, "users"."omniauth_data" AS t1_r25, "users"."memo" AS t1_r26, "users"."access_token" AS t1_r27, "users"."end_user_agreement_accepted_at" AS t1_r28, "users"."privacy_policy_accepted" AS t1_r29, "users"."data" AS t1_r30, "users"."marketing_consent" AS t1_r31, "users"."approved" AS t1_r32, "users"."confirmed" AS t1_r33, "users"."uuid" AS t1_r34 FROM "tasks" LEFT OUTER JOIN "users" ON "users"."id" = "tasks"."responsible_user_id" LEFT OUTER JOIN (
			 SELECT d.task_id, COUNT(*) task_attachment_count
			 FROM   attachments d
			 GROUP BY d.task_id
		   ) c ON c.task_id = tasks.id WHERE ((tasks.archived = FALSE)) AND "tasks"."account_id" = $1  [["account_id", 121]]

And with the latter I get: Missing attribute: responsible_user_id.

lremes avatar Jan 13 '21 14:01 lremes

It seems that the unwanted behaviour is caused by the LEFT OUTER JOIN + select below. For some reason, in Rails 6.1.1 the select() below causes all other "Task" attributes to be dropped from the select.

joins(
%{
		   LEFT OUTER JOIN (
			 SELECT d.task_id, COUNT(*) task_attachment_count
			 FROM   attachments d
			 GROUP BY d.task_id
		   ) c ON c.task_id = tasks.id
		 }
		).select("COALESCE(c.task_attachment_count, 0) AS task_attachment_count")

lremes avatar Jan 13 '21 17:01 lremes

I managed to fix the problem by monkey patching a select_append() method to ActiveRecord::QueryMethods as described here: https://stackoverflow.com/questions/41399788/use-rails-select-to-add-not-overwrite-selected-attributes

This fixed the problem. So it seems that the gem may really not be needed anymore.

Anyway, thanks for the gem and your work maintaining it! It was a lifesaver when it was still needed.

lremes avatar Jan 13 '21 17:01 lremes