views:

95

answers:

1

Part of a real estate application I'm writing will allow a user to subscribe to a location and if a new property becomes available in that location then the user will receive an email notifying them of this.

I plan on runnning a background process once every few hours to handle the matching.

Right now I have a model called location and the plan is to add another model called notification. The location model has a latitude and longitude and so will the notification model.

Something like:

create_table "locations", :force => true do |t|
    t.decimal  "lat",            :precision => 15, :scale => 10
    t.decimal  "lng",            :precision => 15, :scale => 10
end

create_table "notifications", :force => true do |t|
    t.decimal  "lat",            :precision => 15, :scale => 10
    t.decimal  "lng",            :precision => 15, :scale => 10
    t.string   "user_name"
    t.string   "user_email"
end

The obvious thing to do is loop through the list of locations and if one matches that in a notification then send a mail to the user_email defined in the notification model.

What I'm trying to avoid is sending the same email about a location to the same user more than once. What with the process running every few hours.

I thought of adding another field to the notification model called "has_been_mailed" which would be set to true once a mail is sent but then that means the won't get any future updates of other locations added.

Does anyone have any suggestions as to the best way to implement this?

A: 

Without changing your model much, you should have the timestamp fields created_at and updated_at in both tables.

With that, you could query only for locations and notifications that have been updated since the last time you ran your mailer script.

kch
I completely forgot I had created_at and updated_at fields. Perfect. Thanks.
KJF