2

我有...

/app/models/input.rb

class Input < ActiveRecord::Base
  has_many :questions, :dependent => :destroy
  after_commit :create_matching_questions

  def create_matching_questions
    @element_id = Element.all.select{|e| e.meta == true}.first.id
    @standard_id = Standard.all.select{|s| s.meta == true}.first.id
    @description = ["Does the site stock ", self.name, "?"].join
    Product.all.each do |product|
      @question = product.questions.find_or_create_by_element_id_and_standard_id_and_description!(@element_id, @standard_id, @description)
      self.questions << @question
      @question.fields.find_or_create_by_name("The site sells this product and it is in stock")
      @question.fields.find_or_create_by_name("The site sells this product but it is not in stock")
      @question.fields.find_or_create_by_name("The site does not sell this product")
      @question.update_attributes :active => true
    end
    return true
  end

end

/app/models/question.rb

class Question < ActiveRecord::Base
  belongs_to :input
  after_commit :create_matching_surveys

  def create_matching_surveys
    if self.active == true
      self.reload.product.reviews.each do |review|
        review.competitors.each do |competitor|
          (1..self.iterations).each do |iteration|
            survey = competitor.surveys.find_or_create_by_question_id_and_iteration!(self.id, iteration)
            survey.save
          end
        end
      end
      return true
    else
      self.destroy_matching_surveys
    end
  end  

  def destroy_matching_surveys
    self.surveys.each do |survey|
      survey.destroy if survey.question_id == self.id
    end
    return true
  end
end

那为什么我会...

>   @finance = Good.create! :name => "Finance"
 => #<Good id: 6, name: "Finance", created_at: "2013-06-13 02:56:20", updated_at: "2013-06-13 02:56:20"> 

> @super = Input.create! :name => "Superannuation"
 => #<Input id: 11, name: "Superannuation", mispelling: nil, typo: nil, created_at: "2013-06-13 02:56:28", updated_at: "2013-06-13 02:56:28"> 

> @first = @super.questions.first
 => #<Question id: 48, standard_id: 1, description: "Does the site stock Superannuation?", element_id: 2, condition_id: nil, blueprint_name: nil, blueprint_url: nil, additive: false, instructions: nil, created_at: "2013-06-13 02:56:41", updated_at: "2013-06-13 02:56:41", active: false, postscript: "<p>If you have any comments about this question or ...", iterations: 1, product_id: 1, precondition_id: nil, input_id: 11> 

> @last = @super.questions.last
 => #<Question id: 60, standard_id: 1, description: "Does the site stock Superannuation?", element_id: 2, condition_id: nil, blueprint_name: nil, blueprint_url: nil, additive: false, instructions: nil, created_at: "2013-06-13 02:56:43", updated_at: "2013-06-13 02:56:43", active: false, postscript: "<p>If you have any comments about this question or ...", iterations: 1, product_id: 23, precondition_id: nil, input_id: 11> 

> @super.destroy
 => #<Input id: 11, name: "Superannuation", mispelling: nil, typo: nil, created_at: "2013-06-13 02:56:28", updated_at: "2013-06-13 02:56:28"> 

> @super.destroyed?
 => true 

> @first.destroyed?
 => false 

> @last.destroyed?
 => false

当然@first并且@last应该自动销毁?

4

1 回答 1

1

我有同样的问题,解决了它:dependent => :delete_all而不是:dependent => :destroy.

:delete_all不会从控制器调用 destroy 方法并直接从数据库中删除数据。

于 2013-06-13T08:42:34.587 回答