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

Server 模型中的destroy!方法以前可以工作,但现在不能正常工作。它应该找到所有Services与 关联的Server,触发Resque任务(有效),然后销毁Server及其关联的Services

然而,正在发生的事情是它破坏了所有ServerServices(字面意思是整个表),而不仅仅是与Server对象关联的那些,它破坏了所有的关联。我在这里有什么明显的遗漏吗?

4

1 回答 1

0

This was being caused by a broken postgresql sequence 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.

于 2012-06-21T16:40:50.590 回答