@bradc, good question. There's no way to override the default :order by passing the options. To change this behavior you need to reverse it so the :order statement is in with_scope and the options are passed directly to the find method.
I typically reverse the custom find methods so that I also have the option of specifying the type of find I want to do (i.e. :first, :all, array of ids, etc.). So my code would look something like:
<code>
<pre>
def self.find_incomplete(*args, &blk)
with_scope :find => {:conditions => ['complete = ?', false],
:order => 'created_at DESC'} do
find *args, &blk
end
end
</pre>
</code>
The &blk is not really necessary but if helps future-proof the interface.
I was having problems using will_paginate with scope_out. Another plugin "has_finder" seems to provide similar functionality only plays nicely with pagination:
I went through many of the original RailsCasts about a year ago. They where are a great way to refresh on the basics, but rails has changed so much that it would be difficult for a new rails hopeful, such as my son, to make some of them work. Thanks for giving us a fresh, back-to-basics episode.
The documentation says that the :find keywords “may include the :conditions, :joins, :include, :offset, :limit, and :readonly options”. Note that this does not mean that only those options are supported. :sort also works like it should, for example.
his beautiful 4th edition of Modern Combat 4 was well designed by the developers, they have changed a lot of options and they have made this game more interesting and special to gamers.
If I specify an :order option when calling find_incomplete, will with_scope override the one that's already defined by default?
@bradc, good question. There's no way to override the default :order by passing the options. To change this behavior you need to reverse it so the :order statement is in with_scope and the options are passed directly to the find method.
I typically reverse the custom find methods so that I also have the option of specifying the type of find I want to do (i.e. :first, :all, array of ids, etc.). So my code would look something like:
<code>
<pre>
def self.find_incomplete(*args, &blk)
with_scope :find => {:conditions => ['complete = ?', false],
:order => 'created_at DESC'} do
find *args, &blk
end
end
</pre>
</code>
The &blk is not really necessary but if helps future-proof the interface.
I was having problems using will_paginate with scope_out. Another plugin "has_finder" seems to provide similar functionality only plays nicely with pagination:
http://zargony.com/2007/10/20/paginating-special-queries-with-hasfinder-and-will_paginate/
Just as reference, here is the official API Doc:
http://api.rubyonrails.org/classes/ActiveRecord/Base.html#method-c-with_scope
Ryan, is with_scope deprecated in rails 2.3.9? Cuz I get the NoMethodError:Undefined method 'with_scope'
Sorry, I was using with_scope in a controller. And that's why that error. My bad.
This episode is now updated for Rails 5 as a blog post Chaining ActiveRecord::Relation Methods in Rails 5
I went through many of the original RailsCasts about a year ago. They where are a great way to refresh on the basics, but rails has changed so much that it would be difficult for a new rails hopeful, such as my son, to make some of them work. Thanks for giving us a fresh, back-to-basics episode.
I think it's still handy to go through these older railscasts to have a good perspective about where rails has been, where it's come, and why.
The documentation says that the :find keywords “may include the :conditions, :joins, :include, :offset, :limit, and :readonly options”. Note that this does not mean that only those options are supported. :sort also works like it should, for example.
I was having problems with scope_out.
The documentation says that the :find keywords “may include the :conditions, :joins, :include, :offset, :limit, and :readonly options”.
his beautiful 4th edition of Modern Combat 4 was well designed by the developers, they have changed a lot of options and they have made this game more interesting and special to gamers.
Where I am annoying to get to is existence able to chain a sequence of scopes, then smear pagination so I can output pages of alarms.
asem
buset
This is great, I'm new to ruby! Found this really helpful.
Nice episode.like this...Still, I am thinking one thing.
preketek
asem kecut