我有继承自Vibe::Interaction::SurveyMessage的子类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
endclass 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发布于 2020-01-17 20:48:56
您可以使用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发布于 2020-01-17 16:43:09
我设法解决了它,在父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虽然不雅致,但效果很好。如果有人有更好的解决方案我很想听。
https://stackoverflow.com/questions/59778717
复制相似问题