NoBrainerORM / nobrainer

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

:attr.include => value requires array #162

Closed piemont closed 9 years ago

piemont commented 9 years ago

I'm trying to form a Model.where query using an array field (tags) and the :attr.include predicate.

When I use :attr.eq with an array value it returns the document in question:

~/myapp (development) > Post.where(:tags.eq ["nyt","wildfires"]).to_a
[  32.8ms] r.table("posts").order_by({"index" => r.asc(:id)}).filter {|var_21| var_21[:tags].eq(["nyt", "wildfires"])} -- perf: filtering without using an index
=> [
    [0] #<Post id: 0> {
           :content => "http://www.nytimes.com/2015/09/15/us/amid-california-wildfires-escapes-and-choices.html",
        :created_at => 2015-09-14 21:20:10 -0700,
                :id => 0,
              :tags => [
            [0] "nyt",
            [1] "wildfires"
        ],
        :updated_at => 2015-09-14 21:20:10 -0700
    }
]

However when I use :attr.include with a string value it throws an InvalidType error:

~/myapp (development) > Post.where(:tags.include "nyt").to_a
NoBrainer::Error::InvalidType: Tags should be a array
from /Users/dpiemont/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/nobrainer-0.29.0/lib/no_brainer/document/types.rb:32:in `cast_user_to_model_for'

Finally, when I use :attr.include with a single-entry array value, it does not throw the validation error but returns no result (it is clear from the document pasted above this query should return the same result):

~/myapp (development) > Post.where(:tags.include ["nyt"]).to_a
[  33.1ms] r.table("posts").order_by({"index" => r.asc(:id)}).filter {|var_22| var_22[:tags].map {|var_23| var_23.eq(["nyt"])}.contains(true)} -- perf: filtering without using an index
=> []

How can i form a Model.where() with :attr.include with a string value?

nviennot commented 9 years ago

This is indeed a bug. You may use any instead of include for the time being.

piemont commented 9 years ago

OK that works thanks. Consider adding this to the docs at http://nobrainer.io/docs/querying/

nviennot commented 9 years ago

fixed on master :)