销毁rails应用程序中的关联对象

时间:2013-12-05 17:15:02

标签: ruby-on-rails callback rails-activerecord

我正在构建一个rails应用程序。 它由中心和学生组成。一个中心在会议期间有不同的日子。 学生属于一个中心,并选择他们在中心活动时间参加的日子。 我有一个关联设置,如下所示。 销毁CenterDay不会导致StudentDay被破坏。

请帮忙

class Center < ActiveRecord::Base
  has_many :center_days, :dependent => :destroy
  has_many :school_days, :through => :center_days
  accepts_nested_attributes_for :school_days
end

class Student < ActiveRecord::Base
  belongs_to :center
  has_many :student_days, :dependent => :destroy
  has_many :center_days, :through => :student_days
  accepts_nested_attributes_for :center_days
end

class SchoolDay < ActiveRecord::Base
  has_many :center_days
  has_many :centers, :through => :center_days
end

class CenterDay < ActiveRecord::Base
  belongs_to :center
  belongs_to :school_day
  has_many :student_days, :dependent => :destroy
  has_many :students, :through => :student_days
end

class StudentDay < ActiveRecord::Base
  belongs_to :student
  belongs_to :center_day
end

1 个答案:

答案 0 :(得分:0)

您似乎正在使用ActiveRecord'delete'方法来销毁CenterDay对象。 使用'destroy'方法来销毁你的对象,destroy方法执行回调而delete方法没有。

示例:

@center_day.destroy