本文介绍了添加属于与 Ruby Gem Mailboxer 的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个电子商务应用程序,并希望实现类似消息传递系统的功能.在应用程序中,所有对话都将与 Product 模型或 Order 模型相关.在这种情况下,我想将相关对象(类型 + id,我想)存储到 Conversation 对象.

I am building an e-com application and would like to implement something like a messaging system. In the application, all conversation will be related to either a Product model or an Order model. In that case, I would like to store the relating object (type + id, I supposed) to the Conversation object.

要添加字段,我当然可以生成并运行迁移,但是,由于模型和控制器包含在 gem 中,我该如何声明关系?(belongs_to :linking_object, :polymorphic) 和控制器?有什么想法吗?

To add the fields, of course I can generate and run a migration, however, since the Model and Controller are included within the gem, how can I declare the relationship? (belongs_to :linking_object, :polymorphic) and the controller? Any idea?

谢谢.

推荐答案

虽然重写自定义对话系统将是提供自定义要求(例如与其他模型链接)的最佳长期解决方案,以节省一些时间当我使用 ConversationLink 模型实现链接时.我希望它对未来在我职位上的任何人都有用.

Although rewriting a custom Conversation system will be the best long-term solution providing the customization requirement (Like linking with other models for instance), to save some time at the moment I have implement the link with a ConversationLink Model. I hope it would be useful for anyone in the future who are at my position.

模型:conversation_link.rb

class ConversationLink < ActiveRecord::Base
  belongs_to :conversation
  belongs_to :linkingObject, polymorphic: true
end

然后在我希望与 conversation 链接的每个模型中,我只需添加:

then in each models I target to link with the conversation, I just add:

has_many :conversation_link, as: :linkingObject

这只会让你从链接对象中获取相关的对话,但是反向链接的编码可以通过模块中定义的函数来完成.

This will only allow you to get the related conversation from the linking object, but the coding for reverse linking can be done via functions defined in a Module.

这不是一个完美的解决方案,但至少我不需要猴子修补宝石......

This is not a perfect solution, but at least I do not need to monkey patch the gem...

这篇关于添加属于与 Ruby Gem Mailboxer 的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 05:36