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

[FEATURE REQUEST] Make behavior on key conflict in where clause compatible with ActiveRecord #49

Open
a2ikm opened this issue Mar 2, 2021 · 0 comments

Comments

@a2ikm
Copy link
Contributor

a2ikm commented Mar 2, 2021

Currently, Plucky and ActiveRecord generate different queries when calling where clause method twice with the same key, i.g. where(a: 1).where(a: 2).

Plucky generates { :a => { :$in => [1, 2] } }, and it behaves like { :$or => [{ :a => 1 }, { :a => 2 }] }.

require "bundler/inline"

gemfile do
  source "https://rubygems.org"
  gem 'plucky'
end

connection = Mongo::Client.new(["127.0.0.1"], :logger => Logger.new("/dev/null"))
db = connection.use("test").database
collection = db['users']

p Plucky::Query.new(collection).where(a: 1).where(a: 2).criteria_hash
#=> {:a=>{:$in=>[1, 2]}}

On the other hand, ActiveRecord generates "users"."a" = 1 AND "users"."a" = 2, and it returns empty set.

require "bundler/inline"

gemfile do
  source "https://rubygems.org"
  gem "activerecord", "6.1.0"
  gem "sqlite3"
end

require "active_record"

# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new("/dev/null")

ActiveRecord::Schema.define do
  create_table :users, force: true do |t|
    t.integer :a
  end
end

class User < ActiveRecord::Base
end

puts User.where(a: 1).where(a: 2).to_sql
#=> SELECT "users".* FROM "users" WHERE "users"."a" = 1 AND "users"."a" = 2

These behaviors are opposite and can confuse us when building queries with split scoped or methods. So I request to add an option or something like that to make Plucky's behavior compatible with ActiveRecords' one.

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