Michael Schuerig wrote: > On Sunday 10 May 2009, Jian Lin wrote: >> end > Consider > > Phrase.transaction do > frequencies.each do |phrase, freq| > Phrase.create!(:s => phrase, :frequency => freq) > end > end > > Hash#each passes keys and values to your block and avoids unnecessary > lookups. > > My snippet above doesn't take into account your :length => > lengths[phrase] and that is as it should be. Presumably, lengths[phrase] > == phrase.length. Then > > class Phrase < ActiveRecord::Base > attr_protected :length > ... > def s=(value) > self.length = value.length > end > end > > would be much cleaner code because it puts responsibility for setting > the length attribute where it belongs.
i changed it to time_start = Time.now Phrase.transaction do all_phrases.each do |phrase| recordPhrase = Phrase.create!(:s => phrase, :frequency => frequencies[phrase], :length => lengths[phrase]) end end p "took ", Time.now - time_start, " seconds to finish" but it is still the same: it took 75 seconds... i wanted the length, which is the count of word because in the future i might want to do query such as "select * where length > 3" and so if i count the word by getting "s" first, then it will be really slow won't it? if length is stored and indexed, then "length > 3" can be super fast? -- 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@googlegroups.com To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---