2012-06-20 66 views
2

好了,我有2个型号,一个has_many :through关系:依赖摧毁走出去的范围

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 

class Service 
    has_many :servers, :through => :servers_services 
    has_many :servers_services 
end 

这些连接通过:

class ServersService < ActiveRecord::Base 
    belongs_to :server 
    belongs_to :service 
end 

destroy!方法在以前的服务器模型中工作,但现在没有做它应该做的。它应该找到与Server关联的所有Services,触发Resque任务(其工作原理),然后销毁Server及其关联的Services

然而,它正在销毁所有ServerServices(字面上整个表格),而不仅仅是与Server对象关联的对象,它打破了所有关联。有什么明显的我在这里失踪?

回答

0

这是由servers_services表上的ID列上破坏的postgresql序列造成的。修复序列,以便有一个有效的主键,并且一切都按预期再次运行。