You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
With_role cannot be combined into larger queries #579
The definition of resources_find in ResourceAdapter uses (simplified) resources.select("table_name.*"). This hinders the ability of ActiveRecord to combine queries together, because the select is already supplied to select all fields, even when only some of them are needed.
The fix
As far as I understand, removing the complete line that supplies the select does not alter any behavior. It leaves it up to ActiveRecord to determine which fields are necessary for the query. It will default to * but can be limited to id when useful for combining queries. I will try to make a pull request (#580) to see if this is indeed the case in all scenarios. I have tested so far with MySQL and SQLite where it works, and I expect other implementations to behave the same.
A detailed example
Setup
Showing with an example, consider models User, Blog and Article:
These are (usually) combined into a single query. This is because ActiveRecord recognizes that we want only the ids of the blogs and effectively applies .select(:id). Contrast that with for example:
Gives an invalid SQL query. Similarly, applying .select(:id) has no effect and does not affect the (invalid) query (the former select seems to take precedence). The only resort is to use .ids, which splits it into 2 separate queries.
Versions
Rolify 6.0.0
ActiveRecord 6.1.4.6
All versions of Ruby
Actually, #175 explains why this select was added in the first place: as a way to mark the query as not read-only. However, the select prevents the user from specifying their own select (and prevents ActiveRecord from inferring it). Instead, a .readonly(false) might be a cleaner solution.
Not sure if that is necessary though with newer versions of ActiveRecord since that issue is already 8 years old.
Not sure if that is necessary though with newer versions of ActiveRecord since that issue is already 8 years old.
It's an old issue, but not critical. I think it's worth resolving, so we have the ability to override select. In the past 2 months I had to find a workaround for this at least once in a Rails application.
Actually it's quite important, it really complicates queries in larger projects. @thomas-mcdonald you meantioned you're not using this project anymore. I offer my help as I want to use it in production in at least one app (I like this solution so there may be more projects anyway). Can you add me to maintainers of this repo and to rubygems.org gem?
For those who can't wait for a new release, I have been using the following as an initializer in rails to effectively overwrite the behavior from rolify to apply a fix for this problem:
# -----------------------------------------------------------------------------------------------------------------# Redefine the resources_find method to fix a bug in rolify: https://github.com/RolifyCommunity/rolify/issues/579moduleResourceAdapterExtensiondefresources_find(roles_table,relation,role_name)super(roles_table,relation,role_name).except(:select).readonly(false)endendrequire'rolify/adapters/active_record/resource_adapter'Rolify::Adapter::ResourceAdapter.prepend(ResourceAdapterExtension)
Activity
Let rails figure out which fields to select
Taeir commentedon Feb 23, 2022
Actually, #175 explains why this select was added in the first place: as a way to mark the query as not read-only. However, the select prevents the user from specifying their own select (and prevents ActiveRecord from inferring it). Instead, a
.readonly(false)might be a cleaner solution.Not sure if that is necessary though with newer versions of ActiveRecord since that issue is already 8 years old.
wheatevo commentedon Sep 15, 2023
Issue #584 is caused by this select as well.
januszm commentedon Dec 10, 2025
It's an old issue, but not critical. I think it's worth resolving, so we have the ability to override select. In the past 2 months I had to find a workaround for this at least once in a Rails application.
januszm commentedon Dec 26, 2025
Actually it's quite important, it really complicates queries in larger projects. @thomas-mcdonald you meantioned you're not using this project anymore. I offer my help as I want to use it in production in at least one app (I like this solution so there may be more projects anyway). Can you add me to maintainers of this repo and to rubygems.org gem?
Let rails figure out which fields to select (#580)
januszm commentedon Jan 27, 2026
Thanks @thomas-mcdonald ! Also please remember about releasing the gem to rubygems.org
I'll test master branch after the change now.
Taeir commentedon Mar 13, 2026
For those who can't wait for a new release, I have been using the following as an initializer in rails to effectively overwrite the behavior from rolify to apply a fix for this problem: