删除“关联”模型实例时出现问题

时间:2012-01-08 11:20:38

标签: ruby-on-rails ruby ruby-on-rails-3 callback associations

我正在使用Ruby on Rails 3.1.0,我想知道为什么以下示例中的数据库记录在销毁“关联”模型实例时不会被删除。

在我的应用程序中,我有这些类:

class User < ActiveRecord::Base
  has_one :account,
    :autosave    => true,
    :dependent   => :destroy

  has_many :article_relationships,
    :class_name  => 'ArticleUserRelationship',
    :foreign_key => 'user_id',
    :autosave    => true,
    :dependent   => :destroy
  has_many :articles,
    :through     => :article_relationships,
    :source      => :article,
    :dependent   => :destroy

  ...
end

class ArticleRelationship < ActiveRecord::Base
  belongs_to :user

  ...
end

class Account < ActiveRecord::Base
  belongs_to :user,
    :autosave     => true,
    :dependent    => :destroy
end

如果我运行@article.user.destroy@article.user.account.destroy方法(注意:article有点Article),那么用户会从数据库中删除,帐户文章关系记录,但不会删除与用户相关的文章,即使我说明has_many :articles ... :dependent => :destroy

无论如何,我阅读了官方RoR guide about the "has_many Association Reference",并在 4.3.2.6:dependent 章节中说:

  

当您在关联上使用:through选项时,将忽略此选项(:dependent)。

那么,我可以/应该删除所有用户articles?也就是说,我应该/应该运行什么方法?怎么样?

0 个答案:

没有答案
相关问题