Greetings, I''m pretty new to all this. One place I can not find a good example is for deleting a relationship from a HABTM join table. The default method will not work for me because I''m trying to delete the association based on an extra attribute in the table. It seems I need to override the default method with a :delete_sql in my model. Could someone post or point me to an example of a :delete_sql in the model as well as how to pass params to it from the controller? For example, I would like to do something like: model has_and_belongs_to_many :geography, :join_table => "eventgeoglink", :foreign_key => "eventid", :association_foreign_key => "geogid", :delete_sql => "DELETE FROM eventgeoglink WHERE eventid = {eventid} AND geogid = {geogid} and placetypeid = {placetypeid}" controller @event.geography.delete(Geography.find(@params[:geogid]), Placetype.find(@params[:placetypeid])) Many thanks, Scott -- ................................... Scott Gillespie Virginia Center for Digital History
> I''m pretty new to all this. One place I can not find a good example is > for deleting a relationship from a HABTM join table. > > The default method will not work for me because I''m trying to delete the > association based on an extra attribute in the table. It seems I need to > override the default method with a :delete_sql in my model.I submitted a patch (http://dev.rubyonrails.com/ticket/1299) for this not too long ago. You''ll need to be running the latest SVN trunk (or beta gems) for this. has_and_belongs_to_many :active_projects, :join_table => ''developers_projects'', :delete_sql => ''DELETE FROM developers_projects WHERE active=1 AND developer_id = #{id} AND project_id = #{record.id}'' id is the ID of the current model this method is called in, and record is the other model. Obviously, you should be able to access other methods of it, not just id.> Could someone post or point me to an example of a :delete_sql in the > model as well as how to pass params to it from the controller?Passing params may be a bit tricky though. AR treats habtm associations as an array I believe. You may need to make another model that has_many :events and has_many :geography. -- rick http://techno-weenie.net
Thanks Rick. I had found your previous post, but am still missing one piece I think. What does the line in your controller look like that executes the delete? Rick Olson wrote:>>Could someone post or point me to an example of a :delete_sql in the >>model as well as how to pass params to it from the controller? > > > Passing params may be a bit tricky though. AR treats habtm > associations as an array I believe. You may need to make another > model that has_many :events and has_many :geography. >-- ................................... Scott Gillespie Project Manager Virginia Center for Digital History 434.924.4777 (phone) 434.243.5566 (fax)