使用父方法而不是其自己的方法滚动子对象

问题描述 投票:0回答:2

我有从Vibe::Interaction::SurveyMessage继承的Child类Interaction。它们共享同一张表,并且SurveyMessage{app: :vibe, kind:"survey"}标识。

控制器通过父类调用方法:Interaction.find(param[:id]).refresh

问题是,即使对象是SurveyMessage Object,它也会使用父级的update_message方法(空方法)。

是否有一种方法可以迫使对象充当通过父类(SurveyMessage Object)实例化它的Interaction?还是有一种方法可以通过父类识别对象是否属于子类?

class Interaction < ApplicationRecord
  enum app: [ :praise, :review, :vibe, :atlas, :goals ]
  belongs_to :survey, class_name: 'Survey', foreign_key: :vibe_survey_id, required: false
  serialize :attachments 

  def message
    {
      text: self.text,
      attachments: self.attachments
    }
  end

  def update_message
  end

  def refresh(options = {})
    update_message
    h = self.history << {
            :type => :refresh,
            :timestamp => Time.current.to_s
          }
    update( 
      history: h 
    )
    # Submit code
    message
  end

end
class Vibe::Interaction::SurveyMessage < Interaction
  default_scope -> { where(app: :vibe, kind: "survey") }

  def update_message
    msg = survey.answer(user_id, self, additional_options||{} )
    update( text: msg[:text], attachments: msg[:attachments])
  end

end

ruby-on-rails inheritance activerecord ruby-on-rails-5
2个回答
0
投票

我设法解决了在父Interaction上创建路由器方法的问题:

  def self.find_child(id)
    i = Interaction.find_by(id: id)
    if i&.vibe? and i.kind=="survey"
      ans = Vibe::Interaction::SurveyMessage.find(id)
    elsif i&.vibe? and i.kind=="partial"
      ans = Vibe::Interaction::PartialMessage.find(id)
    elsif (etc)
      ...
    else
      ans = i
    end
    ans
  end

它不优雅,但可以。如果有人有更好的解决方案,我很想听听。


0
投票

您可以使用becomes方法

https://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-becomes

如果您的子类具有模式

i = Interaction.find_by(id: id)
i = i.becomes("#{i.kind.capitalize}Message".constantize) if i&.vibe? # or in parent class as a method #downcast
i.refresh
© www.soinside.com 2019 - 2024. All rights reserved.