Jacob
Jacob

Reputation: 6477

How to handle email notifications in a Rails app?

My app has several events based on which a user gets an email notification. What's the best way to handle this from a software/database design perspective?

Here are two instances when I send out an email to a user:

  1. Someone replies to their comment.
  2. Someone likes their comment.

I also need a way for the user to turn these email events off individually in their user settings.

Here is what I'm thinking of doing (which doesn't feel like a good way):

Is there a better more pragmatic way to handle this?

Upvotes: 3

Views: 936

Answers (3)

Moustafa Samir
Moustafa Samir

Reputation: 2268

This is considered a typical user settings where you can save it as a Rails json field or use gems like rails-settings

So assuming you'll use rails-settings gem you can do it as follows:

class User < ActiveRecord::Base
 has_settings :email_notifications
end

then you can set and get settings like this

user.settings(:email_notifications).comments = true
user.settings(:email_notifications).likes = false
user.settings(:email_notifications).comments
# => true

Upvotes: 3

Mangesh Tamhankar
Mangesh Tamhankar

Reputation: 178

One way that I've handled this in the past is to have a separate model for the preferences and for the email type. The preferences table acts as sort of a many to many relationship between users and types where they can set there own preference.

The main advantage here is you can add as many email types as you want (over time) and you wont be clogging up your user model.

Upvotes: 0

Donato Azevedo
Donato Azevedo

Reputation: 1418

I suggest you watch Ryan Bates' Railscasts on activity (ep. #406) and then reading into the public-activity gem. That can be an elegant way to handle the events that send out notifications to users.

As for how you will store and deal with preferences, check out the Preferences gem As the description itself says: ...sometimes it's necessary if you want users to be able to disable things like e-mail notifications.

Upvotes: 0

Related Questions