当有类似的模型时,如何简化我的助手并使其更干燥?
我开始遇到一些困难,使其更加干燥:
它的要点是:
我有一堆模型,contact_email、contact_call、contact_letter 等等。
它们本质上是我实例化与特定联系人实例/记录相匹配的电子邮件模型实例(将其视为模板)的一种方法。
因为它们引用的模型不同,所以我需要一种方法让控制器引用正确的模型。
但这正变得越来越复杂。我尝试了使用“发送”和 Ruby 部分的不同方式来识别关联的类,因此我不需要明确声明它,但运气不佳。
因此——非常不干……救命!
I am starting to have some difficulty making this more DRY:
The gist of it is this:
I have a bunch of Models, contact_email, contact_call, contact_letter, etcetera.
They essential were a way for me to instantiate instances of the Email model (think of it as a template) matched with a specific instance/record of Contact.
Because the Models they reference were different, I needed a way to have the controller reference the right Model.
But this is getting complicated. I played with different ways of using 'send' and parts of Ruby to identify the associated Class so I don't need to explicitly state it, but not having luck.
Thus -- very undry...help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,这是我的第一次攻击:
然后在您的模型中,您将添加一个命名范围:
然后在您的 ContactEmail 模型中添加一个
formatted_status_message
方法:在您的其他模型中:
您将调用电子邮件的方法:
我尝试尽可能多地移动到模型层并利用它,而不是在本例中进行元编程。
well, here is my first whack at it:
And then in your models, you would add a named scope:
and then a
formatted_status_message
method in your ContactEmail model:And in your other models:
and you would call the method for email:
I tried to move as much as possible to the model layer and leverage that, instead of metaprogramming in this case.