Ruby on rails 3 Rails 3.2在销毁前有条件地销毁儿童不开火
我有以下三种型号:Ruby on rails 3 Rails 3.2在销毁前有条件地销毁儿童不开火,ruby-on-rails-3,rails-activerecord,Ruby On Rails 3,Rails Activerecord,我有以下三种型号: class Tag < AR::Base has_many :mention_tags, dependent: :destroy has_many :mentions, through: :mention_tags before_destroy :destroy_mentions def destroy_mentions mentions.each do |mention| mention.destroy if menti
class Tag < AR::Base
has_many :mention_tags, dependent: :destroy
has_many :mentions, through: :mention_tags
before_destroy :destroy_mentions
def destroy_mentions
mentions.each do |mention|
mention.destroy if mention.tags.count == 1
end
end
end
class MentionTag < AR::Base
belongs_to :tag
belongs_to :mention
end
class Mention < AR::Base
has_many :mention_tags
has_many :tags, through: :mention_tags
end
而该规范不符合:
describe 'mention with only this tag' do
before do
@tag = Tag.create(text: 'whatever', conditional: 'yes')
@mention = Mention.create(text: 'whatever', tags: [@tag])
end
it 'should delete the mention when deleting the tag' do
@tag.destroy
Mention.exists?(id: @mention.id).should be_false
end
end
似乎注释掉
标签上的dependent::destroy
会导致所有规范都通过,但我想在删除标签时去掉所有这些规范,因为提及可能会有多个标签,并且不会总是被删除。
describe 'mention with only this tag' do
before do
@tag = Tag.create(text: 'whatever', conditional: 'yes')
@mention = Mention.create(text: 'whatever', tags: [@tag])
end
it 'should delete the mention when deleting the tag' do
@tag.destroy
Mention.exists?(id: @mention.id).should be_false
end
end