2014-02-18 132 views
0

我創建了關聯,其中放置關聯。多態性關聯有關狀態

我需要做多態關聯狀態。

has_one :status, class_name: 'VideoStatus', inverse_of: :video, dependent: :destroy 

belongs_to :video, inverse_of: :status 

我不能做那Association.How做出來嗎?

require 'active_support/concern' 

module EncodeStatuses 
    extend ActiveSupport::Concern 

    FORMATS = %w[mp4] 

    HIGH_VERSION = 'high' 
    MEDIUM_VERSION = 'medium' 
    LOW_VERSION = 'low' 

    VERSIONS = [HIGH_VERSION, MEDIUM_VERSION, LOW_VERSION] 

    included do 
    has_one :status, class_name: 'VideoStatus', inverse_of: :video, dependent: :destroy 
    accepts_nested_attributes_for :status, update_only: true 
    delegate :success?, :failure?, :waiting?, :encoding?, to: :status 
    end 
end 

型號/ video.rb

class Video < ActiveRecord::Base 
    include EncodeStatuses 
    ... 
end 

型號/ post.rb

class Post < ActiveRecord::Base 
    include EncodeStatuses 
    ... 
end 

型號/ video_status.rb

class VideoStatus < ActiveRecord::Base 
    STATUS_WAITING = 'waiting' 
    STATUS_ENCODING = 'encoding' 
    STATUS_SUCCESS = 'success' 
    STATUS_FAILURE = 'failure' 

    STATUSES = [STATUS_WAITING, STATUS_ENCODING, STATUS_SUCCESS, STATUS_FAILURE] 

    belongs_to :video, inverse_of: :status 
    belongs_to :post, inverse_of: :status 
    ... 
end 
+0

做你放置associ 「包含」區塊內的區域? –

+0

@AnilMaurya是的,我只需要做出多態關聯的「地位」。 – user3311412

+0

你能否展示你的關注,以便我們可以調試。 –

回答

0
has_one :status, as: :video_status, dependent: :destroy 

class VideoStatus < ActiveRecord::Base 
    belongs_to :video_status, polymorphic: true 
end 
+0

並且不要忘記將video_status_id,video_status_type添加到video_status表 –

+0

好的,謝謝。我正確地關心了? – user3311412

+0

@ user3311412如果我的回答對您有幫助,那麼請將其標記爲已接受。 –