[Nitro] Og: Custom selects

Michael Neumann mneumann at ntecs.de
Wed Jun 15 13:43:29 EDT 2005

Am Monday 06 June 2005 11:56 schrieb George Moschovitis:
> Hello Michael,
> Document.table
> Returns the table used to map this class. I am thinking about changin
> this to schema (to be more compatible with non-sql backends).
> As for the custom SQL statement. It seems that during the redesign of
> Og I forgot to to include the older 'select' method.
> If you can't wait here is how to do it:
> 	def select(sql, options)
> 		klass = options[:class]
> 		read_all(query(sql), klass, options[:include])
> 	end
> An alternative api will be:
> store.find(:sql => "SELECT * FROM #{Klass.table}", :class => Klass)

Would be nice to have something like this:

Customer.select {|s|  s.category == 'Good' && s.name.like('Maier%') }

This would create the SQL automatically for us:

  "SELECT * FROM #{Customer.table} WHERE category = 'Good' and name like 

And return all matching objects. That's much nicer and cleaner than to create 
the SQL manually, especially because column names might not always match 
property names.

I really would prefer this way. Currently I'm doing the same at runtime like:

  Customer.all.select {|c| c.category == 'Good' && s.name =~ /Maier.*/ }

But this of course is very slow, as it needs to fetch all objects from the 
database before selecting those of interest.



More information about the Nitro-general mailing list