2012-06-02 33 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關聯上設置了依賴選項,那麼銷燬一個關聯記錄的 將具有銷燬其所有同級的漣漪效應。

相關問題