How do I view the change model?

Let's say you have 2 models ...

Store :has_many products 
Product :belongs_to store


.. and the model Product

has an attribute :price


I want to have a view showing users the updated product prices if they drop in price. I'm not sure where to start. What if I want to give users a choice of what products to watch? How would you set this up?


source to share

3 answers

You can start with ActiveRecord observers in your price model to trigger a notification every time the price changes.

You can then turn off these notifications in your subscriber list. There are a number of pub / substructure solutions. One of them is Rails' own built-in notification API, detailed in Jose Walim's book, Rails Application Crafts . Another is the Faye gem , check out their site .

Other parameters are EventMachine, Node.js, or Websockets.



What I would do is add a "trend" field or something that I would update every time the price changes (with a callback or an observer) and set it to "increase / decrease", that makes sense. Then I would look for all products where the trend has been decreasing and that have been updated (according to the updated_at) recently, recently this is the value you define, it could be "in the last two weeks", "since last login user ", etc. etc.



I would use ajax with polling.

On the server side, I will have a background job (choose from backgroundrb, rescue, cron, etc.). Every polling interval I would see if something has changed, and if so, show the content.

I would track what the usng timestamps will change and compare to Now ().



All Articles