Rails STI:调用子方法
我有这样的东西
class Reply < AR::Base
end
class VideoReply < Reply
def hello
p 'not ok'
end
end
class PostReply < Reply
def hello
p 'ok'
end
end
...
所以当我创建对象时:
# params[:reply][:type] = "VideoReply"
@reply = Reply.new(params[:reply])
如何调用子方法(在本例中为VideoReply::hello
)?
UPD: 我只能想象非常愚蠢的解决方案:
@reply = Reply.new(params[:reply])
eval(@reply.type).find(@reply.id).hello
但这并不酷,我认为:)
I've got something like this
class Reply < AR::Base
end
class VideoReply < Reply
def hello
p 'not ok'
end
end
class PostReply < Reply
def hello
p 'ok'
end
end
...
So when I am creating object:
# params[:reply][:type] = "VideoReply"
@reply = Reply.new(params[:reply])
How can I invoke child method (in this case VideoReply::hello
)?
UPD:
I can imagine only very stupid solution:
@reply = Reply.new(params[:reply])
eval(@reply.type).find(@reply.id).hello
But it is not cool, I think :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您处理基于 STI 的模型时,如果不小心的话,创建它们时就会遇到问题。只要您使用基类来查找,检索它们就应该自动完成。
您需要的是首先创建适当类型的模型,其余的就可以了。在您的模型或控制器中定义有效类的列表:
然后您可以在创建对象之前使用它来验证类型:
这应该创建具有正确类的回复。此时方法应该按预期工作。
When you're dealing with STI-based models, you'll have problems creating them if you're not careful. Retrieving them should be done automatically so long as you use the base class to find.
What you need is to create the proper kind of model in the first place and the rest will be fine. In your model or controller define a list of valid classes:
Then you can use this to verify the type before creating an object:
This should create a reply with the proper class. Methods should work as desired at this point.