On Oct 9, 2009, at 19:32 , Nic Benders wrote:

>>> I don't believe that you can pass multiple statements using the
>>> connection.execute method.  Try breaking it up into 4 different
>>> execute statements, wrapped within a transaction if needed.
>>
>> Well, I can't see any way that makes sense.

> There are also AR helpers for the different basic SQL commands that
> will give you slightly more useful return values than the basic
> "execute" does (if you want that information).
>
> For example (with Martin's suggestion of using delete_all):
>
> MicroProvenance.transaction do
>   MicroProvenance.delete_all(selector_n)
>   MicroProvenance.delete_all(selector_b)
>   MicroProvenance.delete_all(selector_g)
>   MicroProvenance.connection.insert(multi_insert_sql)
> end
>
> delete_all also works with named scopes.  So you can DRY up the
> selection quite a bit if you have scopes already setup with something
> like this:
>
> class MicroProvenance < ActiveRecord::Base
>   named_scope :wifi_n_device, :conditions => { :provenable_type =>
> 'Device', :field_name => 'wifi_n_ie' }
>   named_scope :wifi_b_device, :conditions => { :provenable_type =>
> 'Device', :field_name => 'wifi_b_ie' }
>   named_scope :wifi_g_device, :conditions => { :provenable_type =>
> 'Device', :field_name => 'wifi_g_ie' }
> end
>
> and then:
>
>   MicroProvenance.wifi_n_device.delete_all(smaller_selector_n)

Many thanks for all the input.

  In this case, I'm following this up with an INSERT… ON DUPLICATE KEY  
UPDATE for efficiency (one of the few really useful features in MySQL)  
anyway, so It's just as easy extending the SQL generation a little  
further. If I was doing a lot of other things that could use the named  
scopes, sure. But for just this one task, I find the SQL generation  
just as easy to follow.

Maybe I'm just getting old. :-)
--~--~---------~--~----~------------~-------~--~----~
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby
-~----------~----~----~----~------~----~------~--~---

Reply via email to