Adding belongs to relationship to Ruby Gem Mailboxer

前端 未结 4 1986
情话喂你
情话喂你 2021-01-03 10:08

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 Prod

4条回答
  •  没有蜡笔的小新
    2021-01-03 10:24

    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.

    Model: conversation_link.rb

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

    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...

提交回复
热议问题