Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Building Social Features in Ruby on Rails

I'm looking to add the ability for users to message other users in a rails application. I've been looking at several Ruby gems such as "acts_as_messenger", "inboxes", and "mailboxer" and I've had the most success with mailboxer. However, none of those gems has produced a complete solution; mailboxer, for example, allows me to send messages to other users, but the views are very problematic.

Has anybody figured out how to add messaging to their rails application with or without using a ruby gem?

8 Answers

Jody Albritton
PLUS
Jody Albritton
Courses Plus Student 5,497 Points

I have looked at this myself. If you don't have a lot of users yet, don't build this feature. If your going to build it anyway, do it from scratch. You can use a state machine to set the message state. (Read, unread, trash spam, etc). Each message has one sender and multiple recipients.

I have built a fairly robust version of this if you have any questions.

OK, that's pretty helpful. A few questions:

You probably put your state machine inside some sort of message model. What actions did you define in that model?

Surely you must have a related controller and view. What actions did you define other than index, create, new, edit, and destroy?

Also, think I could get a brief rundown of your state machine's code logic?

Jody Albritton
PLUS
Jody Albritton
Courses Plus Student 5,497 Points

Message Model --

class Message < ActiveRecord::Base

  #fields accessible
  attr_accessible :body, :sender_id, :subject, :user_tokens

  #set attribute reader
  attr_reader :user_tokens

  #relationships
  belongs_to :sender, :class_name => 'User'
  has_many :recipients 
  has_many  :users, :through => :recipients


  #create user tokens virtual attribute

  def user_tokens=(ids)
    self.user_ids = ids.split(",")
  end
end

User token grants read access to message object.

Jody Albritton
PLUS
Jody Albritton
Courses Plus Student 5,497 Points

Controller --

class MailboxesController < ApplicationController
  def show
     @user = User.find(current_user)
     @messages = Recipient.where(:user_id => @user)
     @sent_messages = Message.where(:sender_id => @user.id)
     @message = current_user.messages.build(params[:message])
  end
end
Jody Albritton
PLUS
Jody Albritton
Courses Plus Student 5,497 Points

Views can be found here

https://github.com/jodyalbritton/social-demo/tree/master/app/views/mailboxes

Overview of State Machine

https://github.com/pluginaweek/state_machine

It can get very complex rather quickly. You want multiple senders to be able to delete a message without deleting for everyone else. So you use states to set the message read, trashed, etc. Then in the controller just filter for messages of a certain state.

Yea thanks, man! So far, so good

Do you know any tutorials / example apps for building a messaging inbox?