Can someone give me an example of how to override find? I want to setup default order_by conditions and thought this would be a good way to do it. I appear to be having trouble with ruby syntax, and don''t understand how *args stores key => value pairs. Thanks, Dave _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On Tuesday 11 October 2005 06:54, David Clements wrote:> Can someone give me an example of how to override find? I want to > setup default order_by conditions and thought this would be a good > way to do it. I appear to be having trouble with ruby syntax, and > don''t understand how *args stores key => value pairs.Have a look at ActiveRecord::Base#constrain(t). I''m not sure whether it''s in 0.13.1, though. Michael -- Michael Schuerig The Fifth Rider of the Apocalypse mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org is a programmer. http://www.schuerig.de/michael/
On 11-okt-2005, at 12:41, Michael Schuerig wrote:> On Tuesday 11 October 2005 06:54, David Clements wrote: > >> Can someone give me an example of how to override find? I want to >> setup default order_by conditions and thought this would be a good >> way to do it. I appear to be having trouble with ruby syntax, and >> don''t understand how *args stores key => value pairs. >> > > Have a look at ActiveRecord::Base#constrain(t). I''m not sure whether > it''s in 0.13.1, though.That''s a nice method Michael, thanks for pointing out. Pity that you cannot use it as a default (i.e. configure it on class level). As to overwriting find: def self.find(*args) unless args[1] && args[1].respond_to?(:[]) && args[1][:order] args[1] = {} unless args[1] args[1][:order] = ["#{self.table_name}.sortable ASC"] else # logger.warn "Special ordering provisions have been set, if you want to order Cards alphabetically do it yourself" end super(*args) end This adds default ordering by the "sortable" column, but it probably can be overridden. -- Julian "Julik" Tarkhanov
On 10/11/05, Julian ''Julik'' Tarkhanov <listbox-RY+snkucC20@public.gmane.org> wrote:> > On 11-okt-2005, at 12:41, Michael Schuerig wrote: > > > On Tuesday 11 October 2005 06:54, David Clements wrote: > > > >> Can someone give me an example of how to override find? I want to > >> setup default order_by conditions and thought this would be a good > >> way to do it. I appear to be having trouble with ruby syntax, and > >> don''t understand how *args stores key => value pairs. > >> > > > > Have a look at ActiveRecord::Base#constrain(t). I''m not sure whether > > it''s in 0.13.1, though.Yeah I didn''t find this in the docs, maybe a different version.> That''s a nice method Michael, thanks for pointing out. Pity that you > cannot use it as a default (i.e. configure it on class level). > > As to overwriting find: > > def self.find(*args) > unless args[1] && args[1].respond_to?(:[]) && args[1][:order] > args[1] = {} unless args[1] > args[1][:order] = ["#{self.table_name}.sortable ASC"] > else > # logger.warn "Special ordering provisions have been set, > if you want to order Cards alphabetically do it yourself" > end > super(*args) > end > > This adds default ordering by the "sortable" column, but it probably > can be overridden. > > -- > Julian "Julik" TarkhanovGreat thanks, this is what I was looking for. I didn''t realize that *args was an array with the constraints as a Hash in the first element. By knowledge of Ruby has increased now as well. Dave