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() -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Tue, Sep 30, 2008 at 9:13 PM, Anthony E. <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Robby Russell wrote:> On Tue, Sep 30, 2008 at 9:13 PM, Anthony E. > <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> Something like this: >> @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 > > >Thanks Robby. I will be doing this for one model for now, perhaps others down the road. The "Book" model has numeric attributes that I want to save into a "statistics" table so I can draw a line-graph over time from it. Other attributes would be archived as well. Its sounds to be a before_update would give me access to the current price. And I don''t want to keep the current price in a stats table because that table will eventually grow and slow down the regular query. book ---------- id title price edition year stats ---------- id book_id price edition year created_at updated_at Ideally sans-rails the stats table would be populated with a trigger when the book table is updated. There book will definitely "has_many :stats" but I don''t really want that association to be queried every time I get a regular book object, only when I build the stats page. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Book having many :prices does make sense. That way you get to keep the history of the price of the book. Of course, the latest price in the association is the current price. Ramon Tayag On Thu, Oct 2, 2008 at 2:05 AM, Anthony E. <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Robby Russell wrote: >> On Tue, Sep 30, 2008 at 9:13 PM, Anthony E. >> <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >>> Something like this: >>> @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 >> >> >> > > Thanks Robby. > > I will be doing this for one model for now, perhaps others down the > road. > > The "Book" model has numeric attributes that I want to save into a > "statistics" table so I can draw a line-graph over time from it. > > Other attributes would be archived as well. > > Its sounds to be a before_update would give me access to the current > price. And I don''t want to keep the current price in a stats table > because that table will eventually grow and slow down the regular query. > > book > ---------- > id > title > price > edition > year > > stats > ---------- > id > book_id > price > edition > year > created_at > updated_at > > > Ideally sans-rails the stats table would be populated with a trigger > when the book table is updated. > > There book will definitely "has_many :stats" but I don''t really want > that association to be queried every time I get a regular book object, > only when I build the stats page. > > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ramon Tayag wrote:> Book having many :prices does make sense. That way you get to keep > the history of the price of the book. Of course, the latest price in > the association is the current price. > > Ramon TayagThe problem with that is to get the current price (most uses cases) I have to scan the entire ''prices'' table. I''d rather do it in such a way that it only saves the current price, year, edition, etc. to a "stats" whenever the book is updated (without any explicit association). -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Wed, Oct 1, 2008 at 4:01 PM, Anthony E. <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Ramon Tayag wrote: >> Book having many :prices does make sense. That way you get to keep >> the history of the price of the book. Of course, the latest price in >> the association is the current price. >> >> Ramon Tayag > > > The problem with that is to get the current price (most uses cases) I > have to scan the entire ''prices'' table. >*sniff* I smell premature optimization. ;-) Seriously, with database indexes... this is doubtful to be an issue. If it''s ever an issue you can cache the current price in the book model, but really... I wouldn''t be concerned about that for a while.> I''d rather do it in such a way that it only saves the current price, > year, edition, etc. to a "stats" whenever the book is updated (without > any explicit association).This is the case either way. Just not convinced you need to cache this in the Book model from the get-go, which is what you''re leaning toward. 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Robby Russell wrote:> On Wed, Oct 1, 2008 at 4:01 PM, Anthony E. > <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >> have to scan the entire ''prices'' table. >> > > *sniff* I smell premature optimization. ;-) > > Seriously, with database indexes... this is doubtful to be an issue. > If it''s ever an issue you can cache the current price in the book > model, but really... I wouldn''t be concerned about that for a while. > >> I''d rather do it in such a way that it only saves the current price, >> year, edition, etc. to a "stats" whenever the book is updated (without >> any explicit association). > > This is the case either way. Just not convinced you need to cache this > in the Book model from the get-go, which is what you''re leaning > toward. >Thanks Robby, you may indeed be correct. Is it safe to say your recommendation is to create a model for each statistical attribute I want to archive? ie: Price Year Edition ...and associate them to the Book via: has_many and then use the most recent as the current price? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
You can have, on your book table, a price_id that points to it''s latest price. Thus book.price points to its latest price, while book.prices returns all its prices. Do a after_create on Price to set its book''s price_id to itself so that everytime you create a price it''ll set the book''s price_id. Ramon Tayag On Thu, Oct 2, 2008 at 7:01 AM, Anthony E. <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Ramon Tayag wrote: >> Book having many :prices does make sense. That way you get to keep >> the history of the price of the book. Of course, the latest price in >> the association is the current price. >> >> Ramon Tayag > > > The problem with that is to get the current price (most uses cases) I > have to scan the entire ''prices'' table. > > I''d rather do it in such a way that it only saves the current price, > year, edition, etc. to a "stats" whenever the book is updated (without > any explicit association). > > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Anthony E. wrote:> Robby Russell wrote: >> On Wed, Oct 1, 2008 at 4:01 PM, Anthony E. >> <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: >>> have to scan the entire ''prices'' table. >>> >> >> *sniff* I smell premature optimization. ;-) >> >> Seriously, with database indexes... this is doubtful to be an issue. >> If it''s ever an issue you can cache the current price in the book >> model, but really... I wouldn''t be concerned about that for a while. >> >>> I''d rather do it in such a way that it only saves the current price, >>> year, edition, etc. to a "stats" whenever the book is updated (without >>> any explicit association). >> >> This is the case either way. Just not convinced you need to cache this >> in the Book model from the get-go, which is what you''re leaning >> toward. >> > > > > Thanks Robby, you may indeed be correct. > > Is it safe to say your recommendation is to create a model for each > statistical attribute I want to archive? > > ie: > Price > Year > Edition > > ...and associate them to the Book via: has_many and then use the most > recent as the current price?I can''t get the save to work across the association. The price is coming from a backend system un-related to rails so I have got the data itself no problem with "get_price" and saving it to self.price is fine...but I can''t refactor this into saving to another model (ie: prices) Book < ActiveRecord::Base has_many :prices before_save :get_price after_save :set_price def get_price price = Legacy::Data.new().price end At this point my understanding breaks down...how do I save that "get_price" value to the "Prices" model? #brokenzy def set_price @price = self.prices.new @price.price = self.price end -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---