2

The docs between Origin and Mongoid aren't really clear on how to use a Origin::Queryable object. Can I build a Origin::Queryable object up and then pass it to a ModelName.where method? It seems that I can't but on the other hand, seems like a completely sensible thing to do. I can picture a situation where I build a Queryable based on some logic, then pass that to the Model's where clause. Is this possible and I'm just not doing it right?

This is what I'm picturing:

class Criteria
  include Origin::Queryable
end

criteria = Criteria.new
criteria.where(category: 'vacuum').ne(dept: 'home')
vacuums = Product.where(criteria)

That's how I'm thinking it would work, but it doesn't. What do I do with the Criteria object after I build it?

jcollum
  • 43,623
  • 55
  • 191
  • 321
  • Why won't you build criteria from `Product.where`? What difference does this make for your app? (I also don't know what Origin is for :) ) – Sergio Tulentsev Dec 23 '12 at 16:53
  • Origin is the Criteria engine in mongoid 3. When you use a where statement you are building a criteria object. I'm thinking it would be handy to build criteria based on logic, then execute the select. I'm fairly sure you can do this with LINQ in C#. – jcollum Dec 23 '12 at 17:08

1 Answers1

1

So it works like this:

c = Criteria.new
c = c.where(category: 'a').ne(dept: 'home')

Then you have your criteria c , now you just use it, if you are using the 10gen driver you can use like this:

collection.find(c.selector, c.options)

Or using Moped:

session[:collection].find(c.selector).select(c.options.fields)

this should work, just did some local tests and it was fine for me.

Arthur Neves
  • 11,840
  • 8
  • 60
  • 73
  • Is it possible to use origin query via mongoid instances without using mopped. Like described there http://stackoverflow.com/q/14763740/1888017 – Ph0en1x Feb 08 '13 at 00:40