Extend the dialog model in the Mailboxer

I want to expand the Conversation model so that I can use the connection with it. I did this by creating a file called "chat.rb" in the application / model directory this way:

Mailboxer::Conversation.class_eval do
  belongs_to :device, class_name: "Device", foreign_key: 'device_id'
end

I also added a column named "device_id" in the conversation table.

But when I try:

Conversation.last.device

He says:

NoMethodError: undefined method `device' for #<Mailboxer::Conversation:0x007fe83e6ae7c0>
+4
source share
2 answers

, app/models/conversation.rb , Mailboxer::Conversation. , config/initializers/mailboxer.rb. , Rails.application.config.to_prepare, (. Monkey patching Devise ( Rails))

Mailboxer.setup do |config|
  [...]
end

Rails.application.config.to_prepare do
  Mailboxer::Conversation.class_eval do
    belongs_to :device
  end
end

, - current_user.mailbox.conversations.first.device.

+2

class Conversation < Mailboxer::Conversation
  belongs_to :device, class_name: "Device", foreign_key: 'device_id'
end
+1

Source: https://habr.com/ru/post/1547731/


All Articles