如果实例是通过 ActiveRecord 关联定义的,则忽略自定义 method_missing
我提交的内容可能处于各种状态,并编写了一个 method_missing 覆盖,它允许我通过像
submission.draft?
submission.published?
这样的调用来检查它们的状态,效果非常好。
由于各种可能不太好的原因,我还拥有一个名为 Packlet
的模型,该模型属于会议并属于提交。然而,我惊讶地发现它
packlet.submission.draft?
返回了 NoMethodError
。另一方面,如果我将 #draft?
方法硬编码到 Submission
中,则上述方法调用有效。
即使实例是通过 ActiveRecord 关联定义的,如何才能识别出我的 method_missing 方法?
I have submissions that might be in various states and wrote a method_missing override that allows me to check their state with calls like
submission.draft?
submission.published?
This works wonderfully.
I also, for various reasons that might not be so great, have a model called Packlet
that belongs_to a meeting and belongs_to a submission. However, I was surprised to find that
packlet.submission.draft?
returns a NoMethodError
. On the other hand, if I hard-code a #draft?
method into Submission
, the above method call works.
How do I get my method_missing methods to be recognized even when the instance is defined via an ActiveRecord association?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你添加了草稿吗?你的respond_to方法?该对象的方法?我的猜测是,问题可能会出现在那里。当您输入以下内容时会发生什么:
要解决此问题,实际上要编写一个respond_to?像这样的方法:
我的建议是在不使用 method_missing 的情况下实现这个方法,所以通过进行一些像这样的元编程:
Have you added the draft? method to your respond_to? method for that object? My guess would be that the issue might arise there. What happens when you type:
To fix this, actually write a respond_to? method like this:
My recommendation would be to implement this without using method_missing instead though, so by doing some meta-programming like this: