Skip to content

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

Description

@a2ikm

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions