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

Namespaced scopes perform incorrect queries #822

Open
kranzky opened this issue May 15, 2023 · 2 comments
Open

Namespaced scopes perform incorrect queries #822

kranzky opened this issue May 15, 2023 · 2 comments

Comments

@kranzky
Copy link

kranzky commented May 15, 2023

Describe the bug
Namespaced scopes were implemented in #735. They do not work the way I expect.

To Reproduce
Add the following to the spec for namespaced scopes:

it "creates identical queries" do
  expect(MultipleNamespaced.car_unsold.all.to_sql == MultipleNamespaced.unsold.all.to_sql).to be_truthy
  expect(MultipleNamespaced.car_sold.all.to_sql == MultipleNamespaced.sold.all.to_sql).to be_truthy
end

The spec will fail, because the non-namespaced query is:

SELECT "multiple_namespaceds".* FROM "multiple_namespaceds" WHERE "multiple_namespaceds"."status" = 'unsold'

While the namespaced query is:

SELECT "multiple_namespaceds".* FROM "multiple_namespaceds" WHERE "multiple_namespaceds"."status" = 'car_unsold'

Expected behavior
The namespaced query should not be testing a different status value. I do not expect the status values stored in the database to be namespaced.

@kranzky
Copy link
Author

kranzky commented May 15, 2023

I think the fix here would be for create_scope and aasm_create_scope to accept separate scope_name and state_name arguments. For example, the AR implementation might look like this:

conditions = { aasm(state_machine_name).attribute_name => state_name.to_s }
class_eval do
  scope scope_name, lambda { where(table_name => conditions) }
end

Instead of this:

conditions = { aasm(state_machine_name).attribute_name => scope_name.to_s }
class_eval do
  scope scope_name, lambda { where(table_name => conditions) }
end

@kranzky
Copy link
Author

kranzky commented May 15, 2023

See PR #823

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