On Tue, Sep 30, 2008 at 9:13 PM, Anthony E.
<[EMAIL PROTECTED]> wrote:
>
> I'm trying to implement something similar to a historical price chart,
> where I save the prices when they change to a "stats" table.
>
> I'd like to take the current value and save it to a statistics model
> before it gets updated with the new value.
>
> Is there a way to do it with a trigger or should I just save the value
> in two different models?
>
> Something like this:
>
>
> @book = Book.find(params[:id])
> @stats = Stat.new()
>
> puts @book.price #=> $10.00
>
> @stats.book_id = @book.id
> @stats.price = @book.price
>
> @book.price = $20.00
>
> @book.save()
> @stats.save()

Anthony,

Is Book the only model that you'll be doing this for? ...or are there
other models that will use this historical log of prices?

There's a few options here. One, you could add method to be called by
after_save to check the price previously stored with the new one... if
they are different... add a new entry to the historical log.

Another option is to not store prices in the Book model and only in
another table called something like BookPrice.

book has_many book_prices

Then you can scope book_prices for current to do something like:

book.book_prices.current.price  # => get the current price

book.book_prices.each { |book_price| ... }   # => iterate through all
book prices

Anyhow, there's a few options to throw your way. Good luck!

Cheers,
Robby



-- 
Robby Russell
Chief Evangelist, Partner

PLANET ARGON, LLC
design // development // hosting

http://www.planetargon.com/
http://www.robbyonrails.com/
aim: planetargon

+1 503 445 2457
+1 877 55 ARGON [toll free]
+1 815 642 4068 [fax]

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to