#152
Mar 09, 2009

Rails 2.3 Extras

This episode finishes up this series on Rails 2.3. Here you will learn about several smaller additions in 2.3.
Download (12.6 MB, 9:48)
alternative download for iPod & Apple TV (10.3 MB, 9:48)

Resources

gem install rails --source http://gems.rubyonrails.org
# find in batches
Product.count
Product.find_in_batches(:batch_size => 10) do |batch|
  puts "Products in batch: #{batch.size}"
end
Product.each(:batch_size => 10) do |product|
  puts product.name
end

# scoped_by
Product.scoped_by_price(4.99).size
Product.scoped_by_price(4.99).first
Product.scoped_by_price(4.99).scoped_by_category_id(3).first

# try method
Product.find_by_price(4.99).name
Product.find_by_price(4.95).name
Product.find_by_price(4.95).try(:name)
Product.find_by_price(4.99).try(:name)

# product.rb
default_scope :order => "name"

# categories_controller.rb
render 'new'
render 'products/edit'
<!-- products/index.html.erb -->
<%= render @products %>

<!-- products/show.html.erb -->
<%= render @product %>

RSS Feed for Episode Comments 29 comments

1. Ubercow Mar 09, 2009 at 00:46

Very nice, I love the new 2.3 features


2. Carl Mar 09, 2009 at 00:52

Those are some great features. I read a discussion somewhere that if you are using MySQL there can be a performance problem with the way the find in batches works with really large datasets (hundreds of thousands plus).

Many of these are features I can really see some great uses for in my projects already.


3. Tex Mar 09, 2009 at 00:58

Great screencast as always !

Thanks Ryan !

I would like to see a screencast on i18n crud using globalize2 and Rails 2.3 nested form: is it possible ?


4. Boblin Mar 09, 2009 at 01:43

I am waiting for nested models screencast too :)


5. David Pierron Mar 09, 2009 at 02:41

Seems getting this RC2 from the listed source is returning RC1 for me. Is there a cache (DNS or something) that needs clearing to enable this new version for install?


6. Markus Mar 09, 2009 at 04:07

Nice screencast. I'm on edge, but I did't realize that scoped_by_xxx was added (I still have to read the 2.3 release notes), it will clean once for all these one-purpose named scopes in model declaration.

One thing I noted while viewing your screencast is a important "detail" of the method .try I think you din't mention. .try changed from previous releases (I think it was already changed in RC1). Now only returns nil if you try(:something) over Nil, if you try(:something) over any other object, and :something it's not a method of that object, it will raise a NoMethodError. It's an important change to note, so it could lead to odd errors and to a misconception of the method purpose.

Thanks for all your awesome screencasts.

Cheers


7. pimphamster Mar 09, 2009 at 09:39

Safari can’t open the page “http://media.railscasts.com/videos/152_rails_2_3_extras.mov” because it can’t find the server “media.railscasts.com”

:(

I'm guessing there is just lots of traffic today


8. Roger Mar 09, 2009 at 11:00

Thanks for railscasts!! What is the difference between scoped_by and find_by?


9. Stephen Celis Mar 09, 2009 at 11:44

@Roger, you should check out the named_scope Railscast in the show notes. It should answer your question nicely.

The scoped_by method is just a dynamic way to return scopes, whereas find_by and find_all_by query the database and cannot be scoped any further.


10. Stephen Celis Mar 09, 2009 at 11:45

P.S. Nice overview, Ryan! These are the cream of the crop!


11. Allan Mar 09, 2009 at 16:42

Great screencasts.

How can I watch the video from Linux (Ubuntu) ?


12. George Yacoub Mar 09, 2009 at 18:55

Thanks for the screencast Ryan.
+1 for globalize2 and Rails 2.3


13. Jose Mar 10, 2009 at 12:18

Thanks for a great series, again


14. Carl Mar 10, 2009 at 19:32

@Allan,

I use mplayer to watch the screencasts on Ubuntu. You can find some info here http://www.medibuntu.org/ about codecs that are sometimes needed to play some videos on Linux.


15. Allan Mar 11, 2009 at 13:24

@Carl,

Thanks a lot. I'll look for it.

@Ryan,

I love your website design. Simple, pretty and effective. Thanks for all the greats screencasts. It's very useful for me.


16. mk Mar 13, 2009 at 09:58

Hi Ryan,
I must say I really enjoy watching your screencasts and have benefited a lot from them, and I'd like to thank you for taking out time to do them. It can't be easy!

I have one request if you are able: would you be able to do a screencast on receiving email from a rails app?

Thanks and regards.
BTW What is the email that I can get to you on? [Please mail me] :)


17. Andrew Vit Mar 15, 2009 at 13:25

Important: named_scope currently doesn't merge :order options. Hence, neither does default_scope...

The result is that if you set default_scope :order => "name" you will not be able to order by anything else in another scope, even if you specify a different order explicitly:

Product.by_price
Product.this_month :order => 'price'
# still ordered by name!

The only way to change the order is by setting it in the final find call, not a scope:

Product.all :order => 'price'
Product.this_month.all :order => 'price'


18. Ozgur Mar 16, 2009 at 00:34

1)
I guess a nice use of default_scope would be while implementing "single table inheritance" structures.

I always had problems with the way rails handle it, but this way it is done behind the scenes. Perfect!

2)
In addition, I must say that I hated the way find_by_batches works. Splitting the badges according to a condition on id's is a bad idea. What if some records are deleted? Than batch sizes won't be predetermined. I would suggest the use of "LIMIT x, 10" syntax for a batch of size 10.
By the way this is true for MySQL, I don't know about the other DBMSs.


19. justin Perkins Mar 17, 2009 at 17:23

This is for Rails 2.3.2 correct? I've got it running here and can't get the batch methods to work, instead I'm getting an undefined method exception when calling ModelName#each.


20. Bill Burcham Mar 19, 2009 at 11:42

Yeah, I'd like to echo what Ozgur said. Without much analysis it seems to me that depending on object id's filling up the space from 1 up seems like a needless dependency on Rails implementation choices. AR#find already has support for :offset and :limit. Those seem preferable. Here's a class I find useful:

class Incrementally
  
  # Don't process every object in the database -- do it a few at a time
  def self.process( an_active_record_class, at_a_time = 1000)
    objects_count = an_active_record_class.count
    i = 0
    raise "at_a_time must be greater than 0" if at_a_time < 1
    until( i > objects_count)
      objects = an_active_record_class.find( :all, :offset => i, :limit => at_a_time)
      objects.each do |instance|
        if block_given?
          yield instance
        end
      end
      i += at_a_time
    end
  end
end # class

It could be made a lot nicer of course but you get the idea.


21. mikhailov Apr 02, 2009 at 09:01

Excellent screencast!
As I see, the main goal batches process is get the number of objects from model like will_paginate.
Is it right?


22. Marco Apr 10, 2009 at 07:34

Thanks Ryan,

A globalize2 screencast would be Great!


23. Georg Ledermann Aug 17, 2009 at 03:16

Beware that .each has been renamed to .find_each after the screencast was published

http://github.com/rails/rails/commit/04333482bd665e4d4ced167ff4f566ecfc0cc919


24. zirconium Oct 29, 2009 at 02:51

<a href="http://www.stanfordmaterials.com/tungsten.html"><strong>tungsten steel</strong></a>
<a href="http://www.stanfordmaterials.com/zr.html"><strong>zirconium oxide</strong></a>
<a href="http://www.stanfordmaterials.com/ytz.html"><strong>YSZ</strong></a>
<a href="http://www.stanfordmaterials.com/media.html"><strong>grinding media</strong></a>


25. bebeto Oct 30, 2009 at 11:48

http://www.seslisohbetde.net sex porno adult +18 Liseli Porn Lesbian scholl porn baldizsexhikayeleri.


26. Sango Nov 02, 2009 at 21:54

http://blog.163.com/hooljack@126
I am the seo professional monitor.
need some service feel free to contact me

msn:wuzhuhai@live.cn


27. 2366 Nov 10, 2009 at 20:01

It was a very nice idea! Just wanna say thank you for the information you have


28. Preeti Nov 22, 2009 at 02:59

Good screencasts it helps me very much....i have removed most prob..
Thanks again Ryan !


29. wow gold Dec 20, 2009 at 07:59

good,Take a look at Ugg Bailey Button flag is

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