2012-06-02 34 views
1

我的项目中有几个型号。其中有一些是:QualificationCurriculumQualification有孩子(课程表)。我想确保当我删除Qualification我删除了所有的孩子。这里是我的代码:Rails 3 - 依赖=>:delete_all不起作用。为什么?

# Table name: qualifications 
# 
# id   :integer   not null, primary key 
# subject_id :integer 
# teacher_id :integer 
# created_at :datetime  not null 
# updated_at :datetime  not null 
class Qualification < ActiveRecord::Base 
    belongs_to :subject 
    belongs_to :teacher 

    has_many :curriculums, :dependent => :delete_all 
    has_many :school_classes, :through => :curriculums 
end 

# id    :integer   not null, primary key 
# school_class_id :integer 
# qualification_id :integer 
# created_at  :datetime  not null 
# updated_at  :datetime  not null 
class Curriculum < ActiveRecord::Base 
    belongs_to :school_class 
    belongs_to :qualification 

    has_one :result 

    has_many :timetables 
end 

正如你所看到的,我试图在资格模型中使用:dependent => :delete_all。但它不起作用。为什么?

UPD:

我在视图中取消选中复选框删除资格当我编辑:

<div class="control-group"> 
    <%= f.label :subject_ids, "Teacher can teach such subjects in the school", 
       :class => "control-label" %>    

    <div class="controls"> 
    <table class="table table-bordered table-striped"> 
     <thead> 
     <tr> 
      <th>Choose</th> 
      <th>Subject</th> 
     </tr> 
     </thead> 
     <tbody> 
     <%= hidden_field_tag "teacher[subject_ids][]", nil %>       <%# We use hidden field because it doesn't submit unchecked fields. So, we pass nil and nothing will be submitted.%> 

     <% @subjects.each do |subject| %>            
      <tr>                   
      <td>                  
       <%= check_box_tag "teacher[subject_ids][]",        # [] brackets tells that this is array. 
           subject.id,           # Value of checkbox. 
           @teacher.subject_ids.include?(subject.id),   # Here we automatically check checkboxes. 
           id: dom_id(subject) %>        <%# Give unique id for each value. 'dom_id' is Rails helper. We will have ids like: 'subject_1', 'subject_2' and etc. %> 
      </td>                  
      <td>                  
       <%= label_tag dom_id(subject), subject.subject_name %>    <%# Put name of subject. %> 
      </td> 
      </tr> 
     <% end %> 
     </tbody> 
    </table> 
    </div> 
</div> 

下面是详细信息:

class Teacher < ActiveRecord::Base 
    has_many :qualifications 
    has_many :subjects, :through => :qualifications 
end 

class Subject < ActiveRecord::Base 
    has_many :qualifications 
    has_many :teachers, :through => :qualifications 
end 

下面是SQL代码时,我更新我的模型:

Started PUT "/teachers/2" for 127.0.0.1 at 2012-06-03 18:34:44 +0400 
Processing by TeachersController#update as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"ZJNNV9/TO6k18O1Ar1kpkU+PWbd7btHm9Tc067iMNO4=", "teacher"=>{"teacher_last_name"=>"Last", "teacher_first_name"=>"First", "teacher_middle_name"=>"Middle", "teacher_sex"=>"m", "teacher_birthday"=>"1980-12-01", "teacher_phone_attributes"=>{"teacher_mobile_number"=>"88283686", "teacher_home_number"=>"5112787", "id"=>"2"}, "teacher_education_attributes"=>{"teacher_education_university"=>"Mmm", "teacher_education_year"=>"1970-01-01", "teacher_education_graduation"=>"Graduated", "teacher_education_speciality"=>"Math", "id"=>"2"}, "teacher_category"=>"1st", "subject_ids"=>["", "4", "3", "1"]}, "commit"=>"Update", "id"=>"2"} 
    User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = 2 LIMIT 1 
    Teacher Load (0.4ms) SELECT "teachers".* FROM "teachers" WHERE "teachers"."id" = $1 LIMIT 1 [["id", "2"]] 
    (0.1ms) BEGIN 
    Subject Load (0.5ms) SELECT "subjects".* FROM "subjects" WHERE "subjects"."id" IN (4, 3, 1) 
    Subject Load (0.5ms) SELECT "subjects".* FROM "subjects" INNER JOIN "qualifications" ON "subjects"."id" = "qualifications"."subject_id" WHERE "qualifications"."teacher_id" = 2 
    SQL (0.4ms) DELETE FROM "qualifications" WHERE "qualifications"."teacher_id" = 2 AND "qualifications"."subject_id" = 2 
    TeacherPhone Load (0.4ms) SELECT "teacher_phones".* FROM "teacher_phones" WHERE "teacher_phones"."teacher_id" = 2 LIMIT 1 
    TeacherEducation Load (0.4ms) SELECT "teacher_educations".* FROM "teacher_educations" WHERE "teacher_educations"."teacher_id" = 2 LIMIT 1 
    (24.1ms) COMMIT 
Redirected to http://0.0.0.0:3000/teachers 
Completed 302 Found in 57ms (ActiveRecord: 27.2ms) 
+1

你如何删除资格。 –

+0

我更新了我的问题。 – ExiRe

回答

2

尝试:

:dependent => :destroy 

,而不是:dependent => :delete_all

+0

它没有帮助。删除资格后,课程数量相同。 – ExiRe

1

不知道这是你在做什么,但我可以在使用删除家属:依赖=>:DELETE_ALL

而不是使用删除,我用我的控制器销毁。

实施例: 代替: Qualification.delete(PARAMS [:ID])

Use this: 
     Qualification.destroy(params[:id]) 

希望这有助于。 =)

0

了“轨三路”说 -

(第187页) 明确

以事务通过清除外键 场将删除此关联的所有记录(见删除)。如果关联配置为:依赖选项设置为 :delete_all,则它将调用delete_all。同样,如果:dependent选项设置为 :destroy_all,则调用destroy_all方法。

分别删除(*记录),并删除-所有

该删除和DELETE_ALL方法用于切断指定协会,或全部 它们。两种方法都是以交易方式运作 出于性能原因,值得注意的是,调用delete_all首先将整个关联对象集合加载到内存中以获取其ID。然后它 执行一个SQL UPDATE,它将所有当前关联对象的外键设置为零, 将它们与父对象有效地分离。由于它将整个关联加载到内存中,因此对于关联对象的极大集合使用此方法是不明智的。

的删除和DELETE_ALL方法的名称可能会引起误解。默认情况下,它们不会从数据库中删除任何内容 - 它们只能通过清除关联记录的外键 关键字字段来关联关联。此行为与依赖选项有关,其中 默认为:nullify。如果使用:dependent选项将关联配置为 :delete或:destroy,则关联的记录将实际从数据库中删除。

:依赖=>:破坏或:删除

指定相关所有者的记录应视选项的值被破坏或 刚刚删除的数据库,规则。触发时,:destroy将调用 依赖的回调函数,而delete:不会。

此选项的使用可能在has_one/belongs_to配对中有意义。但是, 您希望has_many/belongs_to关系上的此行为不太可能; 它似乎没有道理这样编码的东西。另外,如果所有者 记录在相应的has_many关联上设置了依赖选项,那么销毁一个关联记录的 将具有销毁其所有同级的涟漪效应。

相关问题