2014-10-09 168 views
0

所以我有一個node它可以是不同的類型(一個Comment & Video)。如何從控制器操作中調用另一個控制器操作?

我想要發生的是每當我刪除node時,它也應該刪除它指向的基礎對象 - 例如,一個Video

這裏是我的模型(需要截斷爲簡潔起見):

Node.rb

# == Schema Information 
# 
# Table name: nodes 
# 
# id    :integer   not null, primary key 
# name   :string(255) 
# family_tree_id :integer 
# user_id  :integer 
# media_id  :integer 
# media_type  :string(255) 
# created_at  :datetime 
# updated_at  :datetime 
# circa   :datetime 
# is_comment  :boolean 
# 

class Node < ActiveRecord::Base 
    belongs_to :family_tree 
    belongs_to :user 
    belongs_to :media, polymorphic: true 
    has_many :comments, dependent: :destroy 
    has_many :node_comments, dependent: :destroy 

    def is_video? 
    self.media_type == 'Video' 
    end 

    def comment_count 
    self.node_comments.count + self.comments.count 
    end 
end 

這是我Video.rb

# == Schema Information 
# 
# Table name: videos 
# 
# id   :integer   not null, primary key 
# title  :string(255) 
# description :string(255) 
# yt_video_id :string(255) 
# is_complete :boolean 
# created_at :datetime 
# updated_at :datetime 
# reply_id :integer 
# circa  :datetime 
# 

class Video < ActiveRecord::Base 
    # attr_accessible :title, :description, :yt_video_id, :is_complete, :user_ids 

    has_many :participants, dependent: :destroy 
    has_many :users, through: :participants 
    has_one :node, as: :media, :dependent => :destroy 

    accepts_nested_attributes_for :node, update_only: true 

    def self.yt_session 
    @yt_session ||= YouTubeIt::Client.new(:username => YouTubeITConfig.username , :password => YouTubeITConfig.password , :dev_key => YouTubeITConfig.dev_key) 
    end 

    def self.delete_video(video) 
    yt_session.video_delete(video.yt_video_id) 
    video.destroy 
     rescue 
     video.destroy 
    end 
end 

Comment.rb

# == Schema Information 
# 
# Table name: comments 
# 
# id   :integer   not null, primary key 
# user_id :integer 
# message :text 
# node_id :integer 
# created_at :datetime 
# updated_at :datetime 
# 

class Comment < ActiveRecord::Base 
    validates :message, presence: true 

    belongs_to :user 
    belongs_to :node 

    def self.search(query) 
    where("title like ? OR description like ?", "%#{query}%", "%#{query}%") 
    end 
end 

NodeController#Destroy

def destroy 
    @node.destroy 
    respond_to do |format| 
     format.html { redirect_to root_path } 
     format.json { head :no_content } 
    end 
    end 

VideoController#Destroy

def destroy 
    authorize! :read, @family_tree 
    @video = Video.find(params[:id]) 
    if Video.delete_video(@video) 
     flash[:notice] = "video successfully deleted" 
    else 
     flash[:error] = "video unsuccessfully deleted" 
    end 
    redirect_to dashboard_index_path 
    end 

CommentController#Destroy

def destroy 
    @comment.destroy 
    respond_to do |format| 
     format.html { redirect_to comments_url } 
     format.json { head :no_content } 
    end 
    end 

因此,所有我想要做的是,每當有一個節點中刪除,我希望它刪除相關視頻&評論。

我嘗試添加這對我Node.rb模型,但是當我點擊刪除它返回一個「堆棧級別太深錯誤」:

belongs_to :media, polymorphic: true, dependent: :destroy 

回答

1

我想你想利用dependent: :destroyhas_one :node, as: :media, :dependent => :destroy視頻。 RB。那麼當你在node.rb中使用這一行時,你不應該得到無限遞歸: belongs_to :media, polymorphic: true, dependent: :destroy

+0

Hrmm ...有趣。我仍然想知道如何刪除相關的對象。思考? – marcamillion 2014-10-10 00:37:32

+0

這真的不行嗎? – davidkovsky 2014-10-10 00:55:39

+0

其實......我的壞......你是對的。這確實奏效。謝謝! – marcamillion 2014-10-10 01:09:11

相關問題