Ruby on Rails Thursday, March 29, 2012

Hi all,


I have a table where I'm deleting sparse records -- basically cleaning up broken has_many / belongs_to relationships. (How we get into this situation is a trade-off. We can have '000's of records, and deletes generally happening during peak load, so we just do the clean up later.)

I have ModelA and ModelB. ModelB belongs_to ModelA. ModelA has_many ModelB

Anyway, I have figured out the SQL I want (MySQL:

    DELETE                                                                                                                             
      model_a_table                                                                                                  
    FROM                                                                                                                               
      model_a_table                                                                                                  
      left join model_b_table on model_a_table.model_b_id = model_b_table.id                     
    WHERE                                                                                                                              
      model_b_table.id is NULL;                       

I believe I have the equivalent SELECT figured out as:
ModelA.includes(:model_b).where(:model_b => {:id => nil})

However, I can't seem to get the equivalent delete working. Adding delete_all to the end of the relationship 'breaks' the includes and causes the where to fail.

I'm going to try and figure this out with the old syntax (ModelA.delete(:conditions...)). Thought I'd ask here first as it seems like I should be able to pretty easily convert the SELECT I have to an equivalent delete operation.

Thanks in advance.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/52_KdicA5NIJ.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
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.

No comments:

Post a Comment