#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 8 comments

1. 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.


2. 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.


3. 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.


4. MTH Nov 24, 2008 at 04:37

Please, continue on railscasts about new features.

Especially *internalization*.

Thanks!


5. 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 ...


6. 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


7. jpemberthy Nov 24, 2008 at 05:30

another useful screencast, thanks Ryan! :)


8. 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. :)


9. 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.


10. Chess Nov 24, 2008 at 10:00

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


11. 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.


12. 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.


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

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


14. Nicolas Nov 24, 2008 at 12:48

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


15. 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.


16. 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*).


17. Nicolas Nov 25, 2008 at 07:34

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


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

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

Thanks!


19. 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.

:-)


20. Stefan Nov 26, 2008 at 18:03

Does memoize :find_by_id work too?


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

memoize :what_are_we_eating


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

Many thanks for another useful screencast.

:)


23. 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


24. Dimi Dec 05, 2008 at 17:02

Wll test this one on my own, thanks


25. 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.


26. 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?!


27. 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


28. discount Apr 02, 2010 at 23:35

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


29. replicas handbags Apr 10, 2010 at 05:11

Thank you


30. Gang Apr 25, 2010 at 01:52

Wow, it's very helpful! Thank you.


31. lace wigs May 13, 2010 at 20:23

There is nothing more exciting for both women and young ladies than shopping in RPGSHOW


32. bridesmaid dresses Jun 12, 2010 at 01:57

The post is very nicely written and it contains many useful facts. I am happy to find your distinguished way of writing the post. Now you make it easy for me to understand and implement. Thanks for sharing with us


33. google link building service Jun 14, 2010 at 05:34

Extremely useful - many thanks.


34. 2 Day Diet Japan Lingzhi Jun 26, 2010 at 00:31

I love this page,Really a great page, thanks for your kindly sharing..


35. nfl store Jun 29, 2010 at 17:48

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.


36. w.a. Jul 16, 2010 at 06:51

usefull, thanx


37. DVD to iPad Converter Jul 25, 2010 at 19:23

I don’t know what to say except that I have enjoyed xfg


38. fivefingers kso Jul 26, 2010 at 23:37

fivefingers kso


39. power strip Jul 30, 2010 at 22:29

I think so,it's a good idea.


40. free directory list Aug 11, 2010 at 22:31

Its amazing interesting bangs!


41. Diamond Tools Aug 18, 2010 at 04:42

Good information and pretty thorough as well, do you mind if I reference back to it? I'm blogging about this too, and you have some good info I hadn't seen before and I'd like to post a similar article. I look forward to reading follow ups and hope it stays updated.

<a href="http://www.masterblade.net">Diamond Blades & Tools</a>


42. wholesale new era hats Aug 20, 2010 at 20:53

I recently came across your blog and have been reading along.
I thought I would leave my first comment. I don’t know what to say except that I have enjoyed reading.Nice blog,I will keep visiting this blog very often.


43. jordan air shoes Aug 20, 2010 at 22:57

it’s good to see this information in your post and I really cant wait to read you next posts! I really enjoy watching the RailsCasts. I think type of site that is useful in sharing information and it is important to share. very thanks for this screencast.


44. jimmy choos Aug 21, 2010 at 01:02

This is truly fantastic stuff - thank you too your knowledge


45. lawsuit advance Aug 24, 2010 at 23:31

Really impressed! Everything is very open and very clear explanation of issues. It contains truly information. Your website is very useful. Thanks for sharing. Looking forward to more!


46. louis vuitton shoes Aug 26, 2010 at 21:14

Thanks for sharing your article. I really enjoyed it. I put a link to my site to here so other people can read it. My readers have about the same interets


47. louis vuitton shoes Aug 26, 2010 at 23:21

Thanks for sharing your article. I really enjoyed it. I put a link to my site to here so other people can read it. My readers have about the same interets


48. Wholesale Electronics Aug 27, 2010 at 00:25

Discount Wholesale Electronics, Wholesale Cell Phones, Electronic Gadgets and More from the Best Dropship Wholesaler


49. louis vuitton neverfull Aug 29, 2010 at 23:17

I recently came across your blog and have been reading along.
I thought I would leave my first comment. I don’t know what to say except that I have enjoyed reading.Nice blog,I will keep visiting this blog very often.


50. luxury moblie phone Aug 29, 2010 at 23:20

I enjoyed your article here mate. Infact I’m a fan of the site in general to be very honest. It’s the fourth ocasion I’ve been back here but I kept forgeting to save the site in my saved website list so I have to keep going through the search engines to find it. SAVED this time haha . Best of luck.


51. snow boots Aug 30, 2010 at 21:20

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. :)


52. levis belts Sep 01, 2010 at 20:45

Came across your blog when I was searching bing I have found the bit of info that
I found to be quite useful.


53. dv9000 battery Sep 02, 2010 at 07:41

I really enjoyed it. I put a link to my site to here so other people can read it. My readers have about the same interets

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
Give Back to Open Source