我怎样才能让一个助手在传递给它的不同模型上工作?
我可能需要分两步进行重构,因为我仍在开发项目并学习用例,因为这是为了解决我自己的问题。我有三种模型:信件、电话、电子邮件。它们有一些相似之处,但我预计它们也会有一些不同的属性,正如您从它们的描述中可以看出的那样。
理想情况下,我可以将它们重构为事件,类型为信件、电话、电子邮件,但不知道如何扩展子类。
我的迫切需要是这样的:我有一个助手,可以检查电子邮件(例如)是否发送给特定联系人的状态:
def show_email_status(contact, email)
@contact_email = ContactEmail.find(:first,
:conditions => {:contact_id => contact.id, :email_id => email.id })
if ! @contact_email.nil?
return @contact_email.status
end
end
我意识到我当然想知道是否拨打了电话的状态也是一个联系人,所以我写道:
def show_call_status(contact, call)
@contact_call = ContactCall.find(:first,
:conditions => {:contact_id => contact.id, :call_id => call.id })
if ! @contact_call.nil?
return @contact_call.status
end
end
我希望能够有一个助手 show_status ,我可以在其中说 show_status(contact,call) 或 show_status(contact,email) 并且它会知道是否寻找对象 @ contact_call 或 @contact_email。
是的,如果只是 @contact_event 会更容易,但我想在程序启动和运行时进行一些小的重构,这将使为给定联系人创建历史记录的能力变得更加容易。
谢谢!
注意:目前我将状态作为 contact_email、contact_call 等的属性。Contact_email 仅在发送电子邮件时创建,因此如果尚未发送电子邮件,则不会有 contact_email,并且我需要知道状态是“未发送”...
I am probably going to need to refactor in two steps since I'm still developing the project and learning the use-cases as I go along since it is to scratch my own itch. I have three models: Letters, Calls, Emails. They have some similarilty, but I anticipate they also will have some different attributes as you can tell from their description.
Ideally I could refactor them as Events, with a type as Letters, Calls, Emails, but didn't know how to extend subclasses.
My immediate need is this: I have a helper which checks on the status of whether an email (for example) was sent to a specific contact:
def show_email_status(contact, email)
@contact_email = ContactEmail.find(:first,
:conditions => {:contact_id => contact.id, :email_id => email.id })
if ! @contact_email.nil?
return @contact_email.status
end
end
I realized that I, of course, want to know the status for whether a call was made to a contact as well, so I wrote:
def show_call_status(contact, call)
@contact_call = ContactCall.find(:first,
:conditions => {:contact_id => contact.id, :call_id => call.id })
if ! @contact_call.nil?
return @contact_call.status
end
end
I would love to be able to just have a single helper show_status where I can say show_status(contact,call) or show_status(contact,email) and it would know whether to look for the object @contact_call or @contact_email.
Yes, it would be easier if it were just @contact_event, but I want to do a small refactoring while I get the program up and running, and this would make the ability to do a history for a given contact much easier.
Thanks!
NOTE: Currently I have status as an attribute of contact_email, contact_call, etc. Contact_email only gets created when the email is sent, so there is not contact_email if an email hasn't been sent, and I need to know that the status is "unsent"...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设从示例中,您的关联如下所示:
状态是 ContactEmail / ContactCall 上的一个属性,但您需要电子邮件 / Call 对象的状态,然后(基于此处的 Kandada 答案):
Assuming from the example, that your associations look like this:
and status is an attribute on ContactEmail / ContactCall, but you need a status for Email / Call object, then (based on Kandada answer here):
您可以将助手移至
Contact
模型。现在您可以获得如下状态:
这减轻了对不同助手的需求。解决不同事件的一种方法。
You can move the helper to the
Contact
model.Now you can get the status as follows:
This alleviates the need for different helpers. One method to address different events.
由于您已经有了 show_call_status 和 show_email_status,因此您可以编写第三个 show_letter_status。
然后使用这个:
Since you already have show_call_status and show_email_status, you can write a third show_letter_status.
Then use this: