2017-03-23 40 views
0

我無法刪除嵌套屬性子記錄。 Article_series文章是模型。 這是代碼。無法刪除嵌套屬性子記錄

型號

class Article < ApplicationRecord 
    has_many :articles_article_series 
    has_many :article_series, through: :articles_article_series 

    accepts_nested_attributes_for :articles_article_series, allow_destroy: true, reject_if: proc { |attributes| attributes['article_id'].blank? && attributes['series_id'].blank? && attributes['num'].blank? } 

控制器

def update 
    @article = Article.find(params[:article][:id]) 
    # article_series delete 
    @article.articles_article_series.each do |series| 
     series.mark_for_destruction 
    end 
    @article.save 

有誰告訴我爲什麼&怎麼樣?

+0

你真的要刪除記錄,或者只是想「mark_for_destruction」 – chaitanya

+0

我真的要刪除的記錄。但是'@ article.save'好像不行' –

回答

0

我相信你可能會尋找 has_many :articles_article_series, dependent: :destroy

+0

謝謝,但不幸的是它沒有工作。我只想刪除子記錄,而不是父項。 –

+0

您能否提供一些額外的信息,您想要完成的任務的更具體一些?我很樂意進一步提供更多信息。 – Wasik

+0

太棒了!但不知何故,我終於找到了如何解決它。我會更新我的答案。 –

0

它看起來像只陣列的作品「mark_for_destruction」。

@article.articles_article_series.to_a.first.mark_for_destruction 

之前,我在下面做過。找到解決問題的方法非常困難。

@article.articles_article_series.first.mark_for_destruction 

感謝所有有助於:)