我可能有一个简单的问题。我有以下三个表:
create_table "appointments", force: true do |t|
t.integer "teacher_id"
t.integer "student_id"
t.datetime "start_date"
end
create_table "teachers", force: true do |t|
t.string "name"
end
create_table "students", force: true do |t|
t.string "email"
t.string "first_name"
t.string "last_name"
end
我为这三种类型的数据制作了以下模型:
class Appointment < ActiveRecord::Base
belongs_to :teacher
belongs_to :student
end
class Teacher < ActiveRecord::Base
has_many :appointments
has_many :students, :through => :appointments
end
class Student < ActiveRecord::Base
has_many :appointments
has_many :teachers, :through => :appointments
end
因此,每位教师都可以与许多不同的学生一起预约,每个学生都可以与不同的老师进行多次约会。我的问题是如果管理员从数据库中删除该学生,如何删除属于某个学生的约会?
我以为我正确地建模了这个,但当我删除这样的用户时,他的约会仍在那里:
def destroy
Student.find(params[:id]).destroy
end
谢谢!
答案 0 :(得分:1)
class Student < ActiveRecord::Base
has_many :appointments, :dependent => :delete_all
has_many :teachers, :through => :appointments
end
delete_all
不会调用约会时定义的任何销毁回调。但它是有效的,因为发出一个删除查询