Adding conditions to every find(:all) call

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

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 S.
http://blog.hasmanythrough.com

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

The #with_scope method is good for doing just that, though you should
The 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

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
end

will that work for find_by_something or would you need methods for that
too?

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

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

Alain R. 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’)

Alain

That seems very redundant and not DRY at all.

Alain R. 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

Alain

Right, 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.

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.

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

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