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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
worth noting, since I could have ''000''s of records, the point of this is to avoid having to load the ids I want to delete into the app and let the DB optimize for it. On Thursday, March 29, 2012 1:52:45 PM UTC-7, John Hinnegan wrote:> > 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/-/vdP9isZl99oJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
I am a little confused here. If ModelB belongs to ModelA, I think your
foreign key relationship is backwards. The model_b_table should have a
foreign key to model_a_id, not the other way around.
Regardless, though, you can pass conditions to delete_all which should help
you accomplish what you want.
ModelA.delete_all("model_b_id is null")
--
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/-/yCvn0kpKbqwJ.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks for trying to decipher my problem. I may have made an error in my description of the problem. Regardless, after much digging, what I am looking for is not currently possible. https://github.com/rails/rails/issues/919 On Friday, March 30, 2012 6:43:47 AM UTC-7, Tim Shaffer wrote:> > I am a little confused here. If ModelB belongs to ModelA, I think your > foreign key relationship is backwards. The model_b_table should have a > foreign key to model_a_id, not the other way around. > > Regardless, though, you can pass conditions to delete_all which should > help you accomplish what you want. > > ModelA.delete_all("model_b_id is null") >-- 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/-/1aJeX0CWqkoJ. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.