I have a situation where I need to have all find methods on a object
automatically scoped to a certain condition, ie
MyObject.with_scope( :find => { :conditions => “x = ‘foo’” }) where
‘foo’ is something that is found in the user session.
obviously the model is not going to know anything about the session so
i can’t do something like
class MyObject < ActiveRecord::Base
alias_method :orig_find, :find
def self.find(*args)
with_scope { :find => { :conditions => [“x = ?”, @current_user.x] })
orig_find(*args)
end
end
end
(not even sure if that would be even close to how it MIGHT be
implemented).
i other words, i want to get around having to do
mo = MyObject.find_by_x(x) or having to specify the conditions ALL the
time
any assistance would be appreciated.
Chris
hi,
try the meantime_filter plugin
class BooksController < ApplicationController
meantime_filter :scope_user
public
def index; list; render :action=>‘list’; end
def list
@books = Book.find(:all, :include=>[:user])
end
private
def scope_user
if logged_in?
Book.with_scope(
{:find=>{:conditions=>[“ubooks.user_id = ?”, current_user.id]
}}, &block)
else; yield; end
end
Also look at the scoped_access plugin. See:
http://habtm.com/articles/2006/02/22/nested-with_scope
I’m pretty sure it’s part of Edge Rails and will be standard in the next
major release. The plugin works well for now.
Chris H. wrote:
I have a situation where I need to have all find methods on a object
automatically scoped to a certain condition, ie
MyObject.with_scope( :find => { :conditions => “x = ‘foo’” }) where
‘foo’ is something that is found in the user session.
obviously the model is not going to know anything about the session so
i can’t do something like
class MyObject < ActiveRecord::Base
alias_method :orig_find, :find
def self.find(*args)
with_scope { :find => { :conditions => [“x = ?”, @current_user.x] })
orig_find(*args)
end
end
end
(not even sure if that would be even close to how it MIGHT be
implemented).
i other words, i want to get around having to do
mo = MyObject.find_by_x(x) or having to specify the conditions ALL the
time
any assistance would be appreciated.
Chris