Ok, so I have 2 models with a has_many :through relationship:
class Server < ActiveRecord::Base
has_many :services, :through :servers_services, :dependent => :destroy
has_many :servers_services, :dependent => :destroy
def destroy!
options = {:name => self.name, :services => self.services.map { |s| s.attributes }}
Resque.enqueue(Cluster::DestroyServer, options)
self.destroy
end
end
and
class Service
has_many :servers, :through => :servers_services
has_many :servers_services
end
These are connected through:
class ServersService < ActiveRecord::Base
belongs_to :server
belongs_to :service
end
The destroy! method in the Server model previously worked, but now doesn’t do what it should. It should find all Services associated with the Server, trigger the Resque task (which works) and then destroy the Server and its associated Services.
What is happening however, is that it is destroy ALL ServerServices (literally the whole table), not just the ones associated with the Server object, which breaks all of the associations. Is there something obvious I’m missing here?
This was being caused by a broken
postgresqlsequence on the ID column on the servers_services table. Fixed the sequence so that there is a valid primary key and everything works as expected again.