What is the best solution for applying a default condition to a model,
for example only records where active is boolean true? My initial
implementation overrode the find method using with_scope to apply the
condition, but I soon discovered that didn''t work with the
will_paginate plugin.
I currently have a named_scope called :active that applies the
conditions, but I''m not sure how to incorporate that so that it is
always applied to find and find_by_* without having to use it inside
of the controller.
I''d also like to apply a default sort order and have this code. Could
I follow similar logic for applying the condition?
def self.find(*args)
options = args.last.is_a?(Hash) ? args.pop : {}
if not options.include? :order
options[:order] = ''created_at desc''
end
args.push(options)
super
end
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Craig Demyanovich
2009-Feb-27 05:46 UTC
Re: Applying default conditions to a model''s find, find_by_*
If you can move to Rails 2.3, which is currently in release candidate testing, you could use default scoping: http://ryandaigle.com/articles/2008/11/18/what-s-new-in-edge-rails-default-scoping Regards, Craig --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
ericindc
2009-Feb-27 20:11 UTC
Re: Applying default conditions to a model''s find, find_by_*
Not at this point, so I''d prefer to come up with a solution using 2.2.2. On Feb 27, 12:46 am, Craig Demyanovich <cdemyanov...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> If you can move to Rails 2.3, which is currently in release candidate > testing, you could use default scoping:http://ryandaigle.com/articles/2008/11/18/what-s-new-in-edge-rails-de... > > Regards, > Craig--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Maurício Linhares
2009-Feb-27 20:14 UTC
Re: Applying default conditions to a model''s find, find_by_*
At your model, override the find method:
class SomeModel < ActiveRecord::Base
class << self
def find(*args)
with_scope( :conditions => {:property => ''value''}
) do
super(*args)
end
end
end
end
-
Maurício Linhares
http://alinhavado.wordpress.com/ (pt-br) | http://blog.codevader.com/ (en)
On Fri, Feb 27, 2009 at 5:11 PM, ericindc
<ericmilford-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:>
> Not at this point, so I''d prefer to come up with a solution using
> 2.2.2.
>
> On Feb 27, 12:46 am, Craig Demyanovich
<cdemyanov...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
>> If you can move to Rails 2.3, which is currently in release candidate
>> testing, you could use default
scoping:http://ryandaigle.com/articles/2008/11/18/what-s-new-in-edge-rails-de...
>>
>> Regards,
>> Craig
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
ericindc
2009-Feb-27 21:18 UTC
Re: Applying default conditions to a model''s find, find_by_*
I originally used a with_scope, but that doesn''t play nicely with will_paginate. On Feb 27, 3:14 pm, Maurício Linhares <mauricio.linha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> At your model, override the find method: > > class SomeModel < ActiveRecord::Base > > class << self > > def find(*args) > with_scope( :conditions => {:property => ''value''} ) do > super(*args) > end > end > > end > > end > > - > Maurício Linhareshttp://alinhavado.wordpress.com/(pt-br) |http://blog.codevader.com/(en) > > On Fri, Feb 27, 2009 at 5:11 PM, ericindc <ericmilf...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Not at this point, so I''d prefer to come up with a solution using > > 2.2.2. > > > On Feb 27, 12:46 am, Craig Demyanovich <cdemyanov...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> If you can move to Rails 2.3, which is currently in release candidate > >> testing, you could use default scoping:http://ryandaigle.com/articles/2008/11/18/what-s-new-in-edge-rails-de... > > >> Regards, > >> Craig > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---