我认为您可以通过如下所示的方式设置关联。注意dependent: :destroy
. 当您删除您的用户时,ActiveRecord 将导致dependent: :destroy
对模型的调用并删除与之关联的模型。
class User < ActiveRecord::Base
has_many :emails, through: :user_emails
has_many :emails, dependent: destroy
end
class UserEmail < ActiveRecord::Base
belongs_to :user
belongs_to :email
end
class Email < ActiveRecord::Base
has_many :users, :through: :user_emails
has_many :user_emails, :dependent: :destroy
end
您可以在Polymorphic Associations下查看。希望这会有所帮助
更新查询has_many
关联
@UserEmail = UserEmail.find(:all,
:joins => :user, :email,
:conditions => ["email.email_trashed = ? ", true]
)
不是 100% 确定这会奏效,但我认为这符合他们的要求。