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

Fix scope_for_create to take only equality nodes #41319

Merged
merged 1 commit into from Feb 9, 2021

Conversation

kamipo
Copy link
Member

@kamipo kamipo commented Feb 3, 2021

Related #39683.

If a scope has IN cluase, scope_for_create which is passed to
assign_attributes will include array values, and it will cause weird
behaviors.

That behavior was due to the In node was a subclass of the Equality
node (now the checking has migrated to equality? method).

i.e. It works unless IN queries, for example (BETWEEN):

user = User.where(role: 0..1).find_or_initialize_by(name: "name")

I think that the In node was a subclass of the Equality node is
accidental, so the behavior is almost a bug to me.

This makes scope_for_create takes only equality nodes to fix the
issue.

Fixes #41295.

Related rails#39683.

If a scope has IN cluase, `scope_for_create` which is passed to
`assign_attributes` will include array values, and it will cause weird
behaviors.

That behavior was due to the `In` node was a subclass of the `Equality`
node (now the checking has migrated to `equality?` method).

i.e. It works unless IN queries, for example (BETWEEN):

```ruby
user = User.where(role: 0..1).find_or_initialize_by(name: "name")
```

I think that the `In` node was a subclass of the `Equality` node is
accidental, so the behavior is almost a bug to me.

This makes `scope_for_create` takes only equality nodes to fix the
issue.

Fixes rails#41295.
@kamipo kamipo merged commit cc9814c into rails:main Feb 9, 2021
@kamipo kamipo deleted the fix_scoping_to_take_only_equality branch February 9, 2021 12:31
@lipanski
Copy link

lipanski commented Feb 9, 2021

@kamipo thanks! 🙇

@pixeltrix
Copy link
Contributor

Just linking for information purposes - this change broke the globalize gem when going from 6.1.2 to 6.1.3.

Will patch scope_for_create to work around the change.

pixeltrix added a commit to pixeltrix/globalize that referenced this pull request May 2, 2021
In rails/rails#41319 the `scope_for_create` method was changed so that
it called `where_clause.to_h` directly and now bypasses the patched
version of `where_values_hash`. This means we now have to override
`scope_for_create` in Rails 6.1.3 and later.

Fixes globalize#776
pixeltrix added a commit to pixeltrix/globalize that referenced this pull request May 12, 2021
In rails/rails#41319 the `scope_for_create` method was changed so that
it called `where_clause.to_h` directly and now bypasses the patched
version of `where_values_hash`. This means we now have to override
`scope_for_create` in Rails 6.1.3 and later.

Fixes globalize#776
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Scoping and initializing with IN queries fails for enums
3 participants