NoBrainerORM / nobrainer

Ruby ORM for RethinkDB
http://nobrainer.io/
Other
387 stars 49 forks source link

Unwanted Behaviour of `Model.first_or_create` #151

Closed kuldeepaggarwal closed 9 years ago

kuldeepaggarwal commented 9 years ago

I was using RethinkDB in my recent project and found that first_or_create is not working as it should be. Here is the script to reproduce the issue.

# in first_or_create.rb
require 'rubygems'
require 'nobrainer'

NoBrainer.configure do |config|
  config.rethinkdb_url = "rethinkdb://localhost/dummy_test"
end

class User
  include NoBrainer::Document
  field :name, :uniq => true
  field :email
end

params = { name: 'Test', email: 'kd.engineer@yahoo.co.in' }

User.where(name: params[:name]).first_or_create(params) # this should not raise error.
$ ruby first_or_create.rb
/Users/kuldeepaggarwal/.rvm/gems/ruby-2.2.2/gems/nobrainer-0.27.0/lib/no_brainer/criteria/first_or_create.rb:58:in `_first_or_create': where() and first_or_create() were given conflicting values on the following keys: [:name] (RuntimeError)
    from /Users/kuldeepaggarwal/.rvm/gems/ruby-2.2.2/gems/nobrainer-0.27.0/lib/no_brainer/criteria/first_or_create.rb:5:in `first_or_create'
    from first_or_create.rb:17:in `<main>'

If it is really an issue then I would like to raise a PR to fix this issue.

nviennot commented 9 years ago

Thanks for the detailed bug report :+1: :+1: :smile:

kuldeepaggarwal commented 9 years ago

Now I can use first_or_create in my project. :smile: Thanks for quick fix.