Rick Olson
2005-May-14 19:58 UTC
Specifying :delete_sql on a has_and_belongs_to_many assocation
Hi all, I need specify a custom delete sql query for a has_and_belongs_to_many association in one of my models. The problem is, there''s no reference that I can find that tells me what variables I can use in the query. Here''s what I have so far (simplified from actual code): class Issue < ActiveRecord::Base has_and_belongs_to_many :writers, :delete_sql => ''DELETE FROM creators_issues WHERE issue_id = #{id} AND creator_id = 0'' I figured out I can access the model''s local methods. But, how do I get the ID of the record I''m removing from the habtm collection? I''ve been digging through AR and the association classes but I keep going around in circles. Where''s a good place to start my path to discovery of how AR works? -- rick http://techno-weenie.net
kate rhodes
2005-May-15 00:45 UTC
Re: Specifying :delete_sql on a has_and_belongs_to_many assocation
can''t you just use the clear method to remove all the associations from the join table and add it into your destroy instead of bothering with that? That appears to be what you are doing. -- -Kate (masukomi-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org) On 5/14/05, Rick Olson <technoweenie-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi all, I need specify a custom delete sql query for a > has_and_belongs_to_many association in one of my models. The problem > is, there''s no reference that I can find that tells me what variables > I can use in the query. Here''s what I have so far (simplified from > actual code): > > class Issue < ActiveRecord::Base > has_and_belongs_to_many :writers, :delete_sql => ''DELETE FROM > creators_issues WHERE issue_id = #{id} AND creator_id = 0'' > > I figured out I can access the model''s local methods. But, how do I > get the ID of the record I''m removing from the habtm collection? > > I''ve been digging through AR and the association classes but I keep > going around in circles. Where''s a good place to start my path to > discovery of how AR works? > -- > rick > http://techno-weenie.net > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Rick Olson
2005-May-15 01:34 UTC
Re: Specifying :delete_sql on a has_and_belongs_to_many assocation
On 5/14/05, kate rhodes <masukomi-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> can''t you just use the clear method to remove all the associations > from the join table and add it into your destroy instead of bothering > with that? That appears to be what you are doing.That wasn''t a real example. I used ''creator_id = 0'' because I didn''t know what I could use there. Anyways, I found the bug. It''s patched now in Ticket #1299 (http://dev.rubyonrails.com/ticket/1299). I added a couple tests and added an example to the docs for habtm. -- rick http://techno-weenie.net