Non-optimal sql

I was noticing that to update a has_many association, Rails uses the
following:

begin
delete from tagpairs where id = 33;
delete from tagpairs where id = 34;
delete from tagpairs where id = 35;

insert into tagpairs (…) values (…);
insert into tagpairs (…) values (…);

commit

This would be better as:

delete from tagpairs where obj_type = 'guidepage' and obj_id = 1;
insert into tagpairs (...) values (...);
...

In my case, I’m using a polymorphic association. However, the basic
idea applies to a non-polymorphic association. We can and should delete
members of the association based on the association key field having a
certain value rather than deleting specific ids. Also, notice how this
means that we don’t necessarily have to store the ‘id’ field, which, for
my application, is redundant.

Cs

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs