How would you have an observer / subscribing object to itself to observe a specific area?
In my browser, I'm trying to write a method for a product to "look at itself" and then find the same product with the field :price
being the only difference. In other words, I am trying to notify the user if they subscribe to a product with lower prices.
Here is a table of my products:
create_table :products do |t|
t.string :name
t.decimal :price
t.integer :user_id
t.integer :store_id
t.boolean :watch_price
end
And I have my observer:
class ProductObserver < ActiveRecord::Observer
def after_update(product)
if product.watch_price
end
end
end
So, if boolean is :watch_price
true, the user subscribes to that product. I put it in after_update(product)
because I want users to be able to select a checkbox watch_price
and then it does the Cron job instantly and then every hour after that on the database.
The problem I am facing is to make a product rating myself and look for Products that are the same with the only difference being that it is a field :price
. How to do it?
Thanks in advance, newbie can use some help!
source to share
Set up a subscription engine / relationship. Much could be done to improve the performance of the example below and also make it more modular, but you should get the idea.
app / models / product.rb
class Product < ActiveRecord::Base
has_many :subscriptions
has_many :subscribers, :through => :subscriptions, :class_name => 'User'
end
app / models / user.rb
class User < ActiveRecord::Base
has_many :subscriptions
has_many :products, :through => :subscriptions
def notify_of_price_drop(product, original_price)
# notify somehow
end
end
app / models / subscription.rb
class Subscription < ActiveRecord::Base
# be sure to add snapshot_price field to the subscriptions table in your migration
belongs_to :product
belongs_to :subscriber, :class_name => 'User'
end
app / models / product_observer.rb
class ProductObserver < ActiveRecord::Observer
def after_update(product)
product.subscriptions.each do |subscription|
if subscription.snapshot_price < product.price
subscription.user.notify_of_price_drop(product, subscription.snapshot_price)
end
end
end
end
source to share
I think you will need an HMT watched_products model to keep track of which users are tracking which products. In this model, you can store the attribute of the price they were looking at and the name of the product.
You can use this model to hourly search for lower priced products with the same name and then notify the user.
# Model
class WatchedProduct < ActiveRecord::Base
belongs_to :user
belongs_to :product
# attributes name, price
end
# CRON/Rake task
for watched_product in WatchedProduct.all
for product in Product.find_by_name(watched_product.name)
if watched_product.price > product.price
# notify user
# update the new watched price of the cheaper product
end
end
end
Obviously more can be optimized, but you get the idea.
You probably want to expire viewed products with a different cron job.
source to share