我創建了關聯,其中放置關聯。多態性關聯有關狀態
我需要做多態關聯狀態。
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
做你放置associ 「包含」區塊內的區域? –
@AnilMaurya是的,我只需要做出多態關聯的「地位」。 – user3311412
你能否展示你的關注,以便我們可以調試。 –