I''m trying to add the conditions ''where bucket_id = suchandsuch'' to every Folder.find(:all) call, is there an easy way and DRY way to do this in Rails? Thanks -- 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 -~----------~----~----~----~------~----~------~--~---
Alex MacCaw wrote:> I''m trying to add the conditions ''where bucket_id = suchandsuch'' to > every Folder.find(:all) call, is there an easy way and DRY way to do > this in Rails?The #with_scope method is good for doing just that, though you should only be using it from within the Folder model, not in controller methods. If you want to allow controllers or other models to find all folders in a given bucket, you could make a #bucket_find_all method and call that. I haven''t tried this, but it might work to override the #find method in the Folder class and merge the options there. -- Josh Susser http://blog.hasmanythrough.com -- 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 -~----------~----~----~----~------~----~------~--~---
> The #with_scope method is good for doing just that, though you shouldThe with_scope method is working nicely: def self.find_a(*args) with_scope(:find => { :conditions => ["bucket_id = ?",2] }) do find(*args) end end However, I wish to override the ''find'' method (I don''t want to change every ''find'' to ''find_a''). I get a SystemStackError if I rename ''find_a'' to ''find''. Thanks for your time -- 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 -~----------~----~----~----~------~----~------~--~---
Alex MacCaw wrote:> However, I wish to override the ''find'' method (I don''t want to change > every ''find'' to ''find_a''). I get a SystemStackError if I rename ''find_a'' > to ''find''.Got it, just add super def self.find(*args) with_scope(:find => { :conditions => ["bucket_id = ?",2] }) do super end end -- 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 -~----------~----~----~----~------~----~------~--~---
Alex MacCaw wrote:> Alex MacCaw wrote: >> However, I wish to override the ''find'' method (I don''t want to change >> every ''find'' to ''find_a''). I get a SystemStackError if I rename ''find_a'' >> to ''find''. > Got it, just add super > > def self.find(*args) > with_scope(:find => { :conditions => ["bucket_id = ?",2] }) do > super > end > endwill that work for find_by_something or would you need methods for that too? -- 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 -~----------~----~----~----~------~----~------~--~---
Alex > > with_scope(:find => { :conditions => ["bucket_id = ?",2] }) do > > .. > will that work for find_by_something or would you need methods for that too? As with_scope is on the deprecation path - in version 2.0 -, you should consider replacing every Folder.find(:all) call by a @my_folders = Folder.find(:all, :conditions => ''bucket_id = 123'') , that you can later as you would have used Folder.find(:all) : So, Folder.find_by_name (''john'') becomes @my_folders.find_by_name (''john'') Alain --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Alain Ravet wrote:> As with_scope is on the deprecation path - in version 2.0 -, you > should consider replacing every > Folder.find(:all) > call by a > @my_folders = Folder.find(:all, :conditions => ''bucket_id = 123'') > > , that you can later as you would have used Folder.find(:all) : > > So, > Folder.find_by_name (''john'') > becomes > @my_folders.find_by_name (''john'') > > AlainThat seems very redundant and not DRY at all. -- 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 -~----------~----~----~----~------~----~------~--~---
Justin > > @my_folders = Folder.find(:all, :conditions => ''bucket_id = 123'') > > @my_folders.find_by_name (''john'') > That seems very redundant and not DRY at all. And also very wrong: this code doesn''t work. My bad. I misread the question and mis-reused the usual example of how to replace with_scope : user = User.find(params[:id]) user_articles = user.blog.articles Alain --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Alain Ravet wrote:> Justin > > > > @my_folders = Folder.find(:all, :conditions => ''bucket_id = > 123'') > > > @my_folders.find_by_name (''john'') > > That seems very redundant and not DRY at all. > > > And also very wrong: this code doesn''t work. My bad. > > I misread the question and mis-reused the usual example of how to > replace with_scope : > > user = User.find(params[:id]) > user_articles = user.blog.articles > > > > AlainRight, I use that throughout my code, by i think what hes getting at is how to update things most likely generated by scaffolding without updating the generated code. or if you want to update the scope of something later on after your application already exists. -- 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 -~----------~----~----~----~------~----~------~--~---
You sure that with_scope is on the deprecation path? Last I heard it was just being moved to protected, meaning it can only be used in models. On 11/27/06, Alain Ravet <alain.ravet-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > Alex > > > > with_scope(:find => { :conditions => ["bucket_id = ?",2] }) do > > > .. > > will that work for find_by_something or would you need methods > for that too? > > > As with_scope is on the deprecation path - in version 2.0 -, you > should consider replacing every > Folder.find(:all) > call by a > @my_folders = Folder.find(:all, :conditions => ''bucket_id = 123'') > > , that you can later as you would have used Folder.find(:all) : > > So, > Folder.find_by_name (''john'') > becomes > @my_folders.find_by_name (''john'') > > Alain > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Alex Some people will disagree, but I feel the query scoping you are looking is better located in the controller than in the model. There is a plugin that seems to solve this very problem: meantime_filter Look at the example (3rd screen down) at http://roman2k.free.fr/rails/meantime_filter/0.1.1/rdoc/ It let''s you write code like this : class PostsController < ApplicationController before_filter :authenticate wrap_filter :scope_posts_to_user # Displays the posts of the logged in user. def show @posts = Post.find(:all) end ... Personally, I don''t feel enough pain to use this way, so I''d write it the ''old'' way ,along: my_bucket = Bucket.find(:conditions => ...) all_my_folders = my_bucket.folders Alain --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Brian > You sure that with_scope is on the deprecation path? Last I heard it was > just being moved to protected, meaning it can only be used in models. You''re right. Alain --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---