Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working with two different models #468

Open
RailsCod3rFuture opened this issue Aug 5, 2017 · 3 comments
Open

Working with two different models #468

RailsCod3rFuture opened this issue Aug 5, 2017 · 3 comments

Comments

@RailsCod3rFuture
Copy link

How do you setup Mailboxer to enable cross messaging between two models? Seller and Buyer, etc.

@PhilippeBo
Copy link

Any news about this ?

@ghost
Copy link

ghost commented Mar 17, 2018

I'm not sure if you figured this out yet but since I just did it, I'll add it for others.

I have two models, Client and Trainer.
They both have "name" but neither have an "email field". In this case, a Client object and a Trainer object is really just an informational profile. It's not my User model which has the email field. But Client and Trainer are both associated with a User so it's fine.

This is what I have in both my Client and Trainer models to satisfy Mailboxer:

def name 
    "#{firstname} #{lastname}"
  end
  
  def email
    User.find_by_id(self.user_id)
  end
  
  def mailboxer_email(object)
    email.email
  end
  
  acts_as_messageable

The key to getting this all working smoothly (quite easy actually) is to use name spacing for the two separate models which I will assume you already do. Without launching into a full tutorial on how to setup mailboxer, I would recommend you use this one (https://www.sitepoint.com/messaging-rails-mailboxer/) to set it up for just one of the models like the Seller. Once that is done and working, you just have to copy those files to your Buyer folders.

The tutorial I suggested uses a few new helpers that you will create. This was the tricky part because I had not done that for helpers before but it was pretty simple. I usually only use the application helper and had include_all_helpers set to TRUE. I had to set it to FALSE to get this to work. I needed to create separate helpers for Trainer and Client using the appropriate namespace. Then reference those helpers in the conversations_controller and the conversation_message_controller.

Example of my controller:

class Trainer::ConversationsController < ApplicationController
  before_action :require_login
  before_action :not_authenticated
  around_action :scope_current_account
  before_action :check_trainer_account
  before_action :get_mailbox
  before_action :get_conversation, except: [:index, :empty_trash]
  before_action :get_box, only: [:index]
  
  include Trainer::ConversationsHelper

  ...

So you are just creating two controllers (conversations and messages) for both the Buyer and Seller and using a namespace for each. Then two helpers for each and two separate set of views. You will also need to add routes for each of them in your routes files as well.

So if you do the Seller first. You will want to make sure the messages are being set to the buyer and vice versa for the buyer. Also, I was already using a simple helper for current_trainer and current_client because I needed to for all of the data, etc. So instead of using current_user like in the tutorial, I used those two helpers where appropriate.

I hope this is helpful. It's a bit tough without writing a full tutorial.

@RailsCod3rFuture
Copy link
Author

Can you upload just the project components to Github and post the link? I don’t want to make a mistake and break my solution as is. I just need it for reference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants