#137
Nov 24, 2008

Memoization

Rails 2.2 is out! In this episode I show how to upgrade Rails and then demonstrate one of the new additions: Memoization.
Download (8.7 MB, 5:16)
alternative download for iPod & Apple TV (6.6 MB, 5:16)

Resources

sudo gem install rails
sudo gem update --system
sudo gem install rubygems-update
sudo update_rubygems
gem -v
class Product < ActiveRecord::Base
  extend ActiveSupport::Memoizable
  
  belongs_to :category
  
  def filesize(num = 1)
    # some expensive operation
    sleep 2
    12345789 * num
  end
  memoize :filesize
end

RSS Feed for Episode Comments 34 comments

1. lolcatz Nov 24, 2008 at 00:35

yay! thanks!


2. Ekolguy Nov 24, 2008 at 01:27

Sorry, but the video cannot be played under Windows/Linux neither with latest VLC, nor MP with codecs? Any word on new compression format being used?
Thanks.


3. David Trasbo Nov 24, 2008 at 02:00

Video works just fine here as usual. I'm running QuickTime 7 on Windows XP Pro.

Anyway, this is just what I've been waiting for! I saw the release preparation commit the other day.

Take a look at the Array access core extension: http://github.com/rails/rails/tree/v2.2.2/activesupport%2Flib%2Factive_support%2Fcore_ext%2Farray%2Faccess.rb There is a #forty_two method for accessing "the reddit" :).


4. Dejan Dimic Nov 24, 2008 at 02:20

Video works fine as always. Currently on my Ubuntu but I expect the same on my Xp. Nevertheless, I'll test it.

The subject is on the spot as we all tend to forget the little things that can easily speed up our application.


5. qq Nov 24, 2008 at 02:40

great!


6. Fadhli Rahim Nov 24, 2008 at 03:41

Dejan Dimic, you have to make sure your ubuntu can play restricted media. Google around on how to play media files.


7. MTH Nov 24, 2008 at 04:37

Please, continue on railscasts about new features.

Especially *internalization*.

Thanks!


8. David Trasbo Nov 24, 2008 at 04:38

@Fadhli Rahim: Dejan Dimic didn't have any problems: "Video works fine as always." Ekolguy had, though.


9. cs Nov 24, 2008 at 04:44

I'm using opensuse/gnome and can't watch the video in the browser. I must download and open with MPlayer.

Other .mov videos are shown well from other sites ...


10. Metin Nov 24, 2008 at 05:02

After upgrading to Rails 2.2.2 from a 2.0.2 version I had to remove the following setting from my environment file (development.rb)

config.action_view.cache_template_extensions

The cache_template_extension has been deprecated in ActionView with the latest RoR release, so my server wouldnt startup properly.

Regards


11. jpemberthy Nov 24, 2008 at 05:30

another useful screencast, thanks Ryan! :)


12. Ryan Bates Nov 24, 2008 at 07:16

For those having problems with the video, I'm hoping to add another compression option (non QuickTime) soon. I just haven't been able to get around to it quite yet. Sorry for the inconvenience.

@MTH internationalization is definitely one of the major additions to 2.2, and I will be covering it sometime in this series. :)


13. Kurt Nov 24, 2008 at 09:15

Ubuntu Hardy, can't play either format in any of my 5 video players. Up to this one, the 'alternative download' played more reliably, but I get video without sound on this one (with all 5 players).

I just built mplayer from subversion, and it does exactly the same thing.

Guessing at the content of the video from the show notes is not going to work for me. I wonder if the Peepcode videos will also give me trouble? I guess it's time to pay up and see.


14. Chess Nov 24, 2008 at 10:00

EnvyCasts.com has a great video on Rails 2.2 features.


15. Carl Nov 24, 2008 at 10:34

I was able to play the video under Ubuntu 8.10 using mplayer as long as I didn't resize to full screen (video went blank if I did). and if I paused it when I hit play again it started over from the beginning.


16. Josh Delsman Nov 24, 2008 at 11:32

So, I think it should be made clear that the memoization feature of Rails only works -per- request, not over a span of multiple requests. Therefore, if you call the memoized function on one page, it will not be cached for the next page, or any future pages. This is really only useful if you have to call the same request over and over on a single page (read: partials), etc.

Also, keep in mind that Rails caches database requests automatically, so having a memoized method for anything relating to the database isn't really that useful.

Hope this helps someone! :) Thanks, Ryan.


17. John Yerhot Nov 24, 2008 at 11:57

Short, sweet, and as always - uber useful.
Thanks Ryan.


18. Nicolas Nov 24, 2008 at 12:48

You're my GOD Ryan ! :)
i18n screencast in the near future, great ! ;)


19. Alexei Nov 24, 2008 at 21:49

i18n screencast would be great. AFAIK internationalization in rails will be useful only for titles, headers, some short phrases, rubrics... that is it is for translating interface, not the content of the page. Would be nice if smbd share what he/she uses for fully localizing the app.


20. Clemens Kofler Nov 25, 2008 at 06:53

I wrote a pretty extensive article about the whole topic of Memoization. Maybe this can be an additional source of information for some people: http://www.railway.at/articles/2008/09/20/a-guide-to-memoization (*shameless self-promotion*).


21. Nicolas Nov 25, 2008 at 07:34

Really so great, a serie of screencast on Rails 2.2. :)


22. Sam Millar Nov 25, 2008 at 08:02

Wonderful stuff, had no idea it had been released until I stopped by here!

Thanks!


23. Dejan Dimic Nov 25, 2008 at 12:58

I have tested the video on my XX too and as expected it work like charm.

On my Ubuntu the Totem Movie Player(gstreamer) and on XP the QuickTime player without any problem can play this movie.

To repeat my self in this DRY community: The movie is working fine.

:-)


24. test Nov 25, 2008 at 20:13

test


25. Stefan Nov 26, 2008 at 18:03

Does memoize :find_by_id work too?


26. David Trasbo Nov 27, 2008 at 01:15

@Stefan: Ha, ha. I would consider another form of caching. Interesting idea, though. :)


27. Ivan Acosta-Rubio Nov 28, 2008 at 18:21

memoize :what_are_we_eating


28. Akhil Bansal Nov 30, 2008 at 22:41

Many thanks for another useful screencast.

:)


29. pulkit Dec 03, 2008 at 21:47

Finally rails 2.2 released!
This is what I was waiting for :)
Hey Ryan thanks for all screencasts till yet!
I hope you will put more screencasts ASAP on rails 2.2


30. Dimi Dec 05, 2008 at 17:02

Wll test this one on my own, thanks


31. Trent Dec 10, 2008 at 13:15

This feature was much needed. I liked the "@something ||=" idiom, but it didn't work well in situations that would set the instance variable to null. In those cases, it was kind of ugly to have to write "return @something if defined?(@something)" at the top of the method.


32. PhilT Dec 13, 2008 at 06:21

Video works for me embedded in Firefox on Ubuntu 8.10 after it installed some gstreamer plugins.

After doing update_rubygems on Ubuntu 8.10 I lost all my installed gems (gem list --local) although looking in /var/lib/gems/1.8/bin and .../gems I can see they're there.

Updating rubygems has always been a fun exercise, huh?!


33. PhilT Dec 13, 2008 at 06:23

Oh I see. It's because I was running as sudo instead of root and it's looking in ~/.gem now


34. jordan shoes Jan 06, 2009 at 01:08

good, thanks for your infos

Add your comment:

(SKIP THIS ONE)

(required)

(not shown)


(use pastie or gist for code)

sponsored by:
if you want to help:
required:
Get Quicktime Player