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.
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?
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.
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.
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] :)
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:
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.
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.
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.
Very nice, I love the new 2.3 features
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.
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 ?
I am waiting for nested models screencast too :)
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?
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
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
Thanks for railscasts!! What is the difference between scoped_by and find_by?
@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.
P.S. Nice overview, Ryan! These are the cream of the crop!
Great screencasts.
How can I watch the video from Linux (Ubuntu) ?
Thanks for the screencast Ryan.
+1 for globalize2 and Rails 2.3
Thanks for a great series, again
@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.
@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.
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] :)
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'
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.
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.
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.
Excellent screencast!
As I see, the main goal batches process is get the number of objects from model like will_paginate.
Is it right?
Thanks Ryan,
A globalize2 screencast would be Great!
This feature is a replacement of pseudo_cursors. I just found out after some errors when migrating to Rails 2.3
This episode has been updated to Rails 5 as a blog post Rails 5 ActiveRecord and Partials