There HAS to be an easier way then this, right?....RIGHT? It just seems to take forever. @site = Site.find(params[:id]) @siteproducts = SiteProduct.find(:all, :conditions => [ "site_id = ?", params[:id]]) @sitepurchases = SitPurchase.find(:all, :conditions => [ "site_id = ?", params[:id]]) @sitestats = SiteStat.find(:all, :conditions => [ "site_id = ?", params[:id]]) @site.destroy for product in @siteproducts product.destroy end for purchase in @sitepurchases purchase.destroy end for sitestat in @sitestats sitestat.destroy end --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
tables = %w{site_products site_purchases site_stats} tables.each { |table| ActiveRecord::Base.Connection.execute("truncate #{table}") } be warned that truncates don''t show up in db logs. On Mon, Apr 21, 2008 at 8:02 PM, histrionics <robbucci-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > There HAS to be an easier way then this, right?....RIGHT? > > It just seems to take forever. > > > > @site = Site.find(params[:id]) > @siteproducts = SiteProduct.find(:all, :conditions => [ "site_id > = ?", params[:id]]) > @sitepurchases = SitPurchase.find(:all, :conditions => [ "site_id > = ?", params[:id]]) > @sitestats = SiteStat.find(:all, :conditions => [ "site_id = ?", > params[:id]]) > > @site.destroy > > for product in @siteproducts > product.destroy > end > for purchase in @sitepurchases > purchase.destroy > end > for sitestat in @sitestats > sitestat.destroy > end > > > > >-- Jeff Webb jeff-OBTZZsKxNvRBDgjK7y7TUQ@public.gmane.org http://boowebb.com/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
indeed, but truncate would delete all record from the table whereas I just want to selectively delete those rows that have a site_id column matching the id value passed to the function. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
you could execute the raw sql in that manner by passing in the id to a delete statement then. def delete_site_stuff(site_id) ActiveRecord::Base.connection.execute("delete from site_products where site_id = #{site_id}") end if you''re assured that site_id cannot be injected that would be fine. Otherwise you should run it through the sanitize_sql method. On Mon, Apr 21, 2008 at 8:51 PM, histrionics <robbucci-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > indeed, but truncate would delete all record from the table whereas I > just want to selectively delete those rows that have a site_id column > matching the id value passed to the function. > > >-- Jeff Webb jeff-OBTZZsKxNvRBDgjK7y7TUQ@public.gmane.org http://boowebb.com/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I recommend using delete_all: http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M001385 Post.delete_all "site_id = #{site_id}" -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Apr 21, 2008, at 11:02 PM, histrionics wrote:> There HAS to be an easier way then this, right?....RIGHT? > > It just seems to take forever. > > @site = Site.find(params[:id]) > @siteproducts = SiteProduct.find(:all, :conditions => [ "site_id > = ?", params[:id]]) > @sitepurchases = SitPurchase.find(:all, :conditions => [ "site_id > = ?", params[:id]]) > @sitestats = SiteStat.find(:all, :conditions => [ "site_id = ?", > params[:id]]) > > @site.destroy > > for product in @siteproducts > product.destroy > end > for purchase in @sitepurchases > purchase.destroy > end > for sitestat in @sitestats > sitestat.destroy > endclass Site has_many :site_products, :dependent => :destroy has_many :site_purchases, :dependent => :destroy has_many :site_stats, :dependent => :destroy end @site.destroy Which may be just as slow... Or, if you don''t need to actually instantiate and destroy each record (for example, so callbacks will run), then you can replace the associations with :dependent => :delete_all. That should result in SQL like "DELETE FROM site_products WHERE site_id = #{params[:id]}" -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
try delete_all - http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M001382 which will delete everything in 1 SQL statement. If you need to worry about dependent associations or callbacks try destroy_all which will do the looping for you (but be just as slow). On Apr 21, 10:02 pm, histrionics <robbu...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> There HAS to be an easier way then this, right?....RIGHT? > > It just seems to take forever. > > @site = Site.find(params[:id]) > @siteproducts = SiteProduct.find(:all, :conditions => [ "site_id > = ?", params[:id]]) > @sitepurchases = SitPurchase.find(:all, :conditions => [ "site_id > = ?", params[:id]]) > @sitestats = SiteStat.find(:all, :conditions => [ "site_id = ?", > params[:id]]) > > @site.destroy > > for product in @siteproducts > product.destroy > end > for purchase in @sitepurchases > purchase.destroy > end > for sitestat in @sitestats > sitestat.destroy > end--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
If these are related to one another you could always use something like: has_many :blahs, :dependent => :destroy -- Ryan Bigg http://www.frozenplague.net Feel free to add me to MSN and/or GTalk as this email. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---