【发布时间】:2020-01-16 22:25:23
【问题描述】:
我有继承自Interaction 的子类Vibe::Interaction::SurveyMessage。它们共享同一张表,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
【问题讨论】:
-
试试
Vibe::Interaction::SurveyMessage.find(param[:id]).refresh -
在定义嵌套类时不要使用范围解析运算符 (
::)。这将导致令人讨厌的意外,因为它取决于定义点的模块嵌套。在这种情况下,它是全局范围。所以当你在Vibe::Interaction::SurveyMessage中引用Interaction时,你实际上会得到::Interaction而不是Vibe::Interaction。使用显式嵌套并重新打开模块module Vibe; class Interaction; class SurveyMessage < Interaction ...。 github.com/rubocop-hq/ruby-style-guide#namespace-definition -
嘿@PGill!如果我使用
Vibe::Interaction::SurveyMessage.find(param[:id]),它会起作用,但我想在控制器中使用父Interaction.find(),因为我会有很多不同的孩子。
标签: ruby-on-rails inheritance activerecord ruby-on-rails-5