NoMethodError with empty find results

64 views
Skip to first unread message

hyn

unread,
Sep 7, 2011, 8:38:16 AM9/7/11
to Ohm Ruby
Hi,

For some reason the "empty" Ohm set I get back from find() is not
responding to Enumerable methods. This code works if the results are
non-empty.

ohmset = Post.find(:title => titles)
if ohmset.any?
ohmset.sort_by :title, :limit => titles.length
@results = ohmset.all
end

----
The error:
NoMethodError - undefined method `smembers' for nil:NilClass:
/Users/y/.rvm/gems/ruby-1.9.2-p290/gems/ohm-0.1.3/lib/ohm.rb:473:in
`each'
.../app.rb:170:in `any?'

I'm using ruby 1.9.2

cyx

unread,
Sep 7, 2011, 9:21:34 PM9/7/11
to Ohm Ruby
Hi,

With your example it seems like the error is something deeper.

Can you paste in a gist your whole code for your Post model?

Regards,
cyx

hyn

unread,
Sep 7, 2011, 10:58:09 PM9/7/11
to Ohm Ruby
Hi cyx,

I was actually passing an empty array to find() and expecting it to
return a valid empty ohm set.
find() doesn't appear to like that. Obvious mistake, sorry.

tribalvibes

unread,
Sep 9, 2011, 8:29:07 PM9/9/11
to Ohm Ruby
Hi hyn,

Have you declared title as a plain attribute (e.g., not using Typecast
Array) and are storing an array value? You are correct that find()
does not behave optimally if passed 'nil' or '[]' for the find value.

The behavior of Enumerable find values is changing slightly in 0.2.
Here are several things to be aware of:

find() with an Enumerable (e.g., Array) value in 0.1 operates as AND,
i.e. an intersection:
Post.find( title: ['t1', 't2'] ) will find all Posts where the title
array contains 't1' AND 't2'

However, in 0.2 this is changing to be 't1' OR 't2', in order to
support this broadly applicable functionality, i.e. title IN
[titles].

If you are depending on the 'AND' behavior, in 0.2 the solution is to
use the new Serialized module (evolution of Typecast) and to declare
the attribute explicitly as an Array. Although this preserves the
current behavior, it is a bit inconsistent, so I'm thinking of
removing it altogether. The solution would then be to simply chain the
find calls, Post.find(title: t1).find(title: t2) etc., which is what
it is doing internally anyway.

See https://github.com/soveran/ohm/issues/27 for further details. I'd
appreciate any feedback if you are depending on support for this
behavior.

Note that each value of an Enumerable is individually indexed when the
object is stored. This has not changed. The above relates only to how
find processes its args.

I'll look to fix the find: nil and find: [] to properly return an
empty set.

If you can be on the edge with your project, you could try the 0.2
branch at https://github.com/tribalvibes/ohm and let me know if you
encounter any problems with the above. (This branch still passes the
0.1 tests so there 'should' be minimal differences with other existing
features.)

Cheers,
.tv/
Reply all
Reply to author
Forward
0 new messages