henryturnerlists-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org
2006-Nov-12 15:49 UTC
extending find() in a horrible way..
I''m trying to scope all ActiveRecord::Base.find() calls with a particular condition... ( I want to use some clever language here about domains.. but I''ll mess it up ) Take a look at the monster I created below. Is there a proper way to do this without feeling queasy? This ignores all the dynamic things as well like find_all and find_by_name, which is sad. ( That LinkerConfiguration.current_site.id can be ignored.. just an integer ) cheers -h class LinkCategory < ActiveRecord::Base def self.find(*args) options = args.last.is_a?(Hash) ? args.pop : {} sql = "site_id=''#{LinkerConfiguration.current_site.id}''" if options[:conditions] && !options[:conditions].empty? options[:conditions] += " AND #{sql}" else options[:conditions] = "#{sql}" end args << options super(*args) end 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 -~----------~----~----~----~------~----~------~--~---
henryturnerlists-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org
2006-Nov-12 19:05 UTC
extending find() in a horrible way..
Hi all, I''m trying to extend ActiveRecord::Base.find() to include a default scope with every query... I''ve pasted my dirty attempt below. Surely theres a better way? Another problem with the below is that all the dynamic finder methods aren''t effected. Any ideas? cheers -henry class LinkCategory < ActiveRecord::Base def self.find(*args) options = args.last.is_a?(Hash) ? args.pop : {} sql = "site_id=''#{some_integer}''" if options[:conditions] && !options[:conditions].empty? options[:conditions] += " AND #{sql}" else options[:conditions] = "#{sql}" end args << options super(*args) end 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 -~----------~----~----~----~------~----~------~--~---
henryturnerlists-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org
2006-Nov-13 18:46 UTC
extending find() in a horrible way..
Hi all, I''m trying to add a default scope to all calls to find. My method is really ugly and wondered if anyone had a better solution? It also doesn''t effect all the dynamic finder methods. (I''ve posted this a couple of times but I''m not seeing it in google groups.. not sure if this will work.) cheers -h class LinkCategory < ActiveRecord::Base def self.find(*args) options = args.last.is_a?(Hash) ? args.pop : {} sql = "site_id=''#{some_integer}''" if options[:conditions] && !options[:conditions].empty? options[:conditions] += " AND #{sql}" else options[:conditions] = "#{sql}" end args << options super(*args) end 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 -~----------~----~----~----~------~----~------~--~---
Hi all, I''m trying to add a default scope to all calls to find. My method is really ugly and wondered if anyone had a better solution? It also doesn''t effect all the dynamic finder methods. (I''ve posted this a couple of times but I''m not seeing it in google groups.. not sure if this will work.) cheers -h class LinkCategory < ActiveRecord::Base def self.find(*args) options = args.last.is_a?(Hash) ? args.pop : {} sql = "site_id=''#{some_integer}''" if options[:conditions] && !options[:conditions].empty? options[:conditions] += " AND #{sql}" else options[:conditions] = "#{sql}" end args << options super(*args) end 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 -~----------~----~----~----~------~----~------~--~---
honestly I have no dang idea what thats suppose to do how about some documentation? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Try this: http://roman2k.free.fr/rails/meantime_filter/0.1.2/ it is exactly what you need. pt. On 11/13/06, Henry Turner <henryturnerlists-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> > Hi all, > > I''m trying to add a default scope to all calls to find. > > My method is really ugly and wondered if anyone had a better solution? > It also doesn''t effect all the dynamic finder methods. > > (I''ve posted this a couple of times but I''m not seeing it in google > groups.. not sure if this will work.) > > cheers > -h > > class LinkCategory < ActiveRecord::Base > > def self.find(*args) > options = args.last.is_a?(Hash) ? args.pop : {} > sql = "site_id=''#{some_integer}''" > if options[:conditions] && !options[:conditions].empty? > options[:conditions] += " AND #{sql}" > else > options[:conditions] = "#{sql}" > end > args << options > super(*args) > end > > end > > > >-- Parker Thompson http://www.parkert.com/ 510.541.0125 --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Wow, thanks for the link.... My favourite plugin so far.. cheers -henry On 11/14/06, Parker Thompson <parkert-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Try this: > > http://roman2k.free.fr/rails/meantime_filter/0.1.2/ > > it is exactly what you need. > > pt. > > On 11/13/06, Henry Turner <henryturnerlists-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: > > > > Hi all, > > > > I''m trying to add a default scope to all calls to find. > > > > My method is really ugly and wondered if anyone had a better solution? > > It also doesn''t effect all the dynamic finder methods. > > > > (I''ve posted this a couple of times but I''m not seeing it in google > > groups.. not sure if this will work.) > > > > cheers > > -h > > > > class LinkCategory < ActiveRecord::Base > > > > def self.find(*args) > > options = args.last.is_a?(Hash) ? args.pop : {} > > sql = "site_id=''#{some_integer}''" > > if options[:conditions] && !options[:conditions].empty? > > options[:conditions] += " AND #{sql}" > > else > > options[:conditions] = "#{sql}" > > end > > args << options > > super(*args) > > end > > > > end > > > > > > > > > > -- > Parker Thompson > http://www.parkert.com/ > 510.541.0125 > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
check out "with_scope" http://rubyonrails.com/rails/classes/ActiveRecord/Base.html#M000892 however, read this as well. DHH has decided to make with_scope protected as of 2.0, so it could affect how you use it. http://www.mail-archive.com/rubyonrails-core-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org/msg00579.html On 11/12/06, henryturnerlists-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org <henryturnerlists-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> > Hi all, > > I''m trying to extend ActiveRecord::Base.find() to include a default > scope with every query... I''ve pasted my dirty attempt below. Surely > theres a better way? > > Another problem with the below is that all the dynamic finder methods > aren''t effected. > > Any ideas? > > cheers > -henry > > class LinkCategory < ActiveRecord::Base > > def self.find(*args) > options = args.last.is_a?(Hash) ? args.pop : {} > sql = "site_id=''#{some_integer}''" > if options[:conditions] && !options[:conditions].empty? > options[:conditions] += " AND #{sql}" > else > options[:conditions] = "#{sql}" > end > args << options > super(*args) > end > > 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 -~----------~----~----~----~------~----~------~--~---
On 11/20/06, Chris Hall <christopher.k.hall-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > check out "with_scope" > > http://rubyonrails.com/rails/classes/ActiveRecord/Base.html#M000892 > > however, read this as well. DHH has decided to make with_scope > protected as of 2.0, so it could affect how you use it. > > http://www.mail-archive.com/rubyonrails-core-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org/msg00579.htmldef find_in_site(*args) with_scope :find => { ... } do find *args end end -- Rick Olson http://weblog.techno-weenie.net http://mephistoblog.com --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---