On Wed, 2009-12-23 at 18:44 -0800, Jeremy Evans wrote:
> On Dec 23, 12:36 am, John Anderson <[email protected]> wrote:
> > Hi there
> >
> > I've just started using Sequel on a project. I'm looking for something
> > similar to ActiveRecord's find_by_sql method. Yes, I do actually want
> > to write the SQL statement myself ;-)
> >
> > I create a dataset with the relevant SQL statement, and then using the
> > Model.dataset = method, which works, but that seems quite clunky.
> >
> > I see I could also use def_dataset_method, but that's too permanent
> > for what I want.
> >
> > So is there a good way to do this?
>
> You probably don't want to use Model.dataset with a static SQL string.
>
> Here's a couple of options:
>
> # Create new dataset from a Database object
> dataset = DB['SELECT * FROM table']
>
> # Override SQL for an existing dataset (returns copy of dataset)
> dataset = dataset.with_sql('SELECT * FROM table')
>
> In order to give better advice, you'd have to be more specific in
> terms of what you want.
Thanks. Eventually I did this, which does what I want:
module Sequel
class Model
def self.find_by_sql( statement, &block )
if block.nil?
with_sql( statement ).all
else
with_sql( statement ).each {|obj| yield obj }
end
end
end
end
bye
John
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sequel-talk?hl=en.