0

I am trying to make it so users can subscribe to a forum and when a forum is updated the user is alerted. I can't for the life of me figure out how to set up the database to do it. I am pretty sure you are supposed to set up an associative table with user_id and forum_id but I am not to sure. If someone could give me some pointers on how to start this or plan it out or even point me to some resources that would be great.

Thanks in advance.

G3tinmybelly
  • 1,777
  • 2
  • 15
  • 18

3 Answers3

1

create a join table called user_forum_subscriptions

class UserForumSubscription < ActiveRecord::Base
  self.table_name = "user_forum_subscriptions"
  belongs_to :user
  belongs_to :forum
end

class User < ActiveRecord::Base
  has_many :user_forum_subscriptions
end

class Forum < ActiveRecord::Base
  has_many :user_forum_subscriptions
  has_many :users, :through => :user_forum_subscriptions
end

Now, forum.users are the ones you need to alert when the forum changes

usha
  • 28,973
  • 5
  • 72
  • 93
1

I suppose you should start with generating a new model called Subscription. Look at subscriptions as resources. User should have_many subscriptions which can be added/removed/edited.

I'd probably start with Michael Hartl's tutorial Chapter 11, where he shows how to establish the 'following' relationship for his twitter-like app. In many ways it is similar to what you are trying to accomplish. Here's the link, read about the Relationship model, hope it's what you're looking for.

The Whiz of Oz
  • 6,763
  • 9
  • 48
  • 85
1

I would probably do something like build a table with user_id, forum_id and user_last_notified_date columns.

Then every so often (hourly, perhaps daily) I'd kick off a process that looks for the most recent update in a forum, looks for users subscribed to that forum, then checks the user_last_notified_date column. If the column is blank or prior to the most recent forum update, then send those users a notification.

If the notification needs to be more immediate, then whenever a forum is updated, run basically that same process, probably ignoring the user who made the update (so you don't get notified whenever you make a forum update).

beercodebeer
  • 990
  • 6
  • 5
  • Quick question, how can I make it that after a certain amount of time it will send out notifications? I was looking it up but to no success. – G3tinmybelly Dec 13 '13 at 21:10
  • 1
    I was merely referring to the database side of the equation. For ROR, I'm not sure how to set up a scheduled task, but there must be some mechanism for it. There might be some good information [here](http://stackoverflow.com/questions/553648/what-is-the-best-way-to-schedule-a-sending-email-task-with-ruby-on-rails) – beercodebeer Dec 13 '13 at 21:14