Skip to content
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

Add support for query_constraints (from Rails 7.1) #812

Open
basex opened this issue Oct 25, 2023 · 0 comments
Open

Add support for query_constraints (from Rails 7.1) #812

basex opened this issue Oct 25, 2023 · 0 comments

Comments

@basex
Copy link

basex commented Oct 25, 2023

Rails 7.1 added query_constraints as a way to support multiple attributes when querying associations:
https://edgeapi.rubyonrails.org/classes/ActiveRecord/Persistence/ClassMethods.html#method-i-query_constraints
This is useful to use composite keys, security or just in general for performance improvements.

I made a test with the globalize gem but doesn't work with functions that eager load the translations like with_translations and includes:
I added the required columns to the table with the translations.

Example:

class Category < ApplicationRecord
  query_constraints :store_id, :id
  
  translates :name
end

Without query_constraints:

Category.includes(:translations).all
 #=> Category Load (0.4ms)  SELECT `categories`.`id`, `categories`.`created_at`, `categories`.`updated_at` FROM `categories` /* loading for pp */ LIMIT 11
 #=> Category::Translation Load (2.7ms)  SELECT `category_translations`.* FROM `category_translations` WHERE `category_translations`.`category_id` IN (1, 2, 3, 4, 5, 6, 7)

With query_constraints:

Category.includes(:translations).all
 #=> Category Load (0.4ms)  SELECT `categories`.`id`, `categories`.`created_at`, `categories`.`updated_at` FROM `categories` /* loading for pp */ LIMIT 11
 #=> Category::Translation Load (2.7ms)  SELECT `category_translations`.* FROM `category_translations` WHERE `category_translations`.`category_id` IN (NULL)

Expected with query_constraints:

Category.includes(:translations).all
 #=> Category Load (0.4ms)  SELECT `categories`.`id`, `categories`.`created_at`, `categories`.`updated_at` FROM `categories` /* loading for pp */ LIMIT 11
 #=> Category::Translation Load (2.7ms)  SELECT `category_translations`.* FROM `category_translations` WHERE `category_translations`.`store_id` IN (1, 2, 3) AND `category_translations`.`category_id` IN (1, 2, 3, 4, 5, 6, 7)
@basex basex changed the title Rails 7.1 support for query_constraints Add support for query_constraints (from Rails 7.1) Oct 25, 2023
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

No branches or pull requests

1 participant