Adding condition filter to find records

hey,

All my model have a publish, publish_to, publish_from fields.

when i fetch records (ex Products) with id > 10, i do it like this:
@items = Product.find(:all, :conditions => [‘id > ?’, 10] )

now i want to do an extra filter on all my finds (in the frontend) Like
this

@items = Product.find_publish(:all, :conditions => [‘id > ?’, 10] )

which means this exactly:

stime = Time.now.utc.strftime(’%Y-%m-%d’)
@items = Product.find(:all, :conditions => [‘id > ? AND publish = 1 AND
(publish_from is null OR publish_from <= ?) AND (publish_to is null OR
publish_to >= ?)’,10, stime, stime ])

How can i do this, so that i keep the orginal conditions, and expand it
with 3 other condition…

Thx

I think you might be wanting with_scope

Product.with_scope(:conditions => [“id < ?”, 10]) do
stime = Time.now.utc.strfime("%Y-%m-%d")
@items = Product.find(:all, :conditions => [“publish = 1 and
(publish_from is null or publish_from <= ?) and (publish_to is null or
publish_to >= ?)”, stime, stime])
end

all queries within the block automatically have the defined
conditions, ie, they have been scoped.

http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M000892
http://habtm.com/articles/2006/02/22/nested-with_scope

Chris

Chris H. wrote:

I think you might be wanting with_scope

Product.with_scope(:conditions => [“id < ?”, 10]) do
stime = Time.now.utc.strfime("%Y-%m-%d")
@items = Product.find(:all, :conditions => [“publish = 1 and
(publish_from is null or publish_from <= ?) and (publish_to is null or
publish_to >= ?)”, stime, stime])
end

all queries within the block automatically have the defined
conditions, ie, they have been scoped.

http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M000892
http://habtm.com/articles/2006/02/22/nested-with_scope

Chris

hmm, i have to version in my website, a backend (CMS) and a frontend
(the website)

In the backend i just need the find()
in the frontend i need the public_find() (=find()+the public filter)

It seems this is on model level?? How can i then make the diff between
the CMS end the website… i need something like a module (only in
frontend) thats extends the find…

Thx

Chris H. wrote:

take a look at the second link i listed. scan down the page until you
get to the section titled ‘around filter’. study the example he has
provided as it sounds exactly like what you are trying to accomplish.

Chris

hey, i read the link, and i use this

protected
def mine
{
:find => {:conditions => [“user_id = ?”,
session[:current_user].id]},
:create => {:user_id => session[:current_user].id},
}
end

Mail.with_scope(mine) do
@mails = Mail.find(:all)
end

But i want to use the ScopedAccess::Filter, cuz less repetitive code.
http://habtm.com/articles/2006/02/22/nested-with_scope

class ScopedAccess::Filter
def initialize (klass, method_scoping)

end
def before (controller)
@klass.scoped_methods << @method_scoping # means enable
‘with_scope’
end
def after (controller)
@klass.scoped_methods.pop # means disable
‘with_scope’
end
end

around_filter ScopedAccess::Filter.new(Mail, :mine)

where do i need to define/import the ScopedAccess class?? cant figure it
out :s

Thx

And another thing

i use this code as scope

protected
def publish
{
:find => {:conditions => [‘publish = 1 AND (publish_from is null OR
publish_from <= ?) AND (publish_to is null OR publish_to >= ?)’,
Date.today, Date.today ]},
}
end

but i need this in all my controllers in my frontend, so its also
duplicate code, if i have to put it in every controller…Can this also
be any better?

i cant put it in my appliction controller, because thats is used for my
backend as my frontend.

Thx
N.

what i might do in this case is:

class FrontEndController < ApplicationController
end

class MyRealFrontEndController < FrontEndController
end

class MyOtherRealFrontEndController < FrontEndController
end

and put your frontend code in the FrontEndController class

take a look at the second link i listed. scan down the page until you
get to the section titled ‘around filter’. study the example he has
provided as it sounds exactly like what you are trying to accomplish.

Chris

just put the ScopedAccess class in your models directory.

Chris H. wrote:

just put the ScopedAccess class in your models directory.

i always get this problem
uninitialized constant ScopedAccess

c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:123:in
const_missing' c:/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:133:inconst_missing’
#{RAILS_ROOT}/app/controllers/frontend/products_controller.rb:2

this is my code, what is it that i do wrong?

\app\controllers\frontend\products_controller.rb:

class Frontend::ProductsController < ApplicationController
around_filter ScopedAccess::Filter.new(Product, :publish)

protected
def publish
{
:find => {:conditions => [‘publish = 1 AND (publish_from is null OR
publish_from <= ?) AND (publish_to is null OR publish_to >= ?)’,
Date.today, Date.today ]},
}
end

public
def index
redirect_to :action => ‘list’
end

def list
	@items = Product.find(:all, :order => "name ASC")
end

end

\app\models\scoped_access.rb:

class ScopedAccess::Filter
def initialize (klass, method_scoping)
#…
end
def before (controller)
@klass.scoped_methods << @method_scoping # means enable
‘with_scope’
end
def after (controller)
@klass.scoped_methods.pop # means disable
‘with_scope’
end
end

Thanks
N.

Hey,

(me again :p)

I am pretty new to this multi application controller stuff, always used
1 app controller.

i made a new controller front_end_controller.rb

app\controllers\front_end_controller.rb:

class FrontEndController < ApplicationController
before_filter :test

def test()
	p "TESTING TESTING TESTING TESTING TESTING TESTING TESTING TESTING 

TESTING TESTING TESTING TESTING TESTING TESTING TESTING TESTING "
end
end

How can I run this??
When I surf to http://localhost:3000/frontend/products/list
I dont see the TESTING stuff in the console…

This is my folder structure:
F:\rails_template_0.0.2\app\controllers

F:\rails_template_0.0.2\app\controllers\application.rb
F:\rails_template_0.0.2\app\controllers\backend
F:\rails_template_0.0.2\app\controllers\frontend
F:\rails_template_0.0.2\app\controllers\front_end_controller.rb

F:\rails_template_0.0.2\app\controllers\backend\faqs_controller.rb
F:\rails_template_0.0.2\app\controllers\backend\faq_questions_controller.rb
F:\rails_template_0.0.2\app\controllers\backend\fsdocuments_controller.rb
F:\rails_template_0.0.2\app\controllers\backend\fsimages_controller.rb
F:\rails_template_0.0.2\app\controllers\backend\login_controller.rb
F:\rails_template_0.0.2\app\controllers\backend\module_permissions_controller.rb
F:\rails_template_0.0.2\app\controllers\backend\products_controller.rb
F:\rails_template_0.0.2\app\controllers\backend\roles_controller.rb
F:\rails_template_0.0.2\app\controllers\backend\users_controller.rb

F:\rails_template_0.0.2\app\controllers\frontend\products_controller.rb

Thanks
N.

Chris H. wrote:

did you take a look at the plugin he wrote? probably much easier than
writing it all yourself.

ruby script/plugin install
http://wota.jp/svn/rails/plugins/branches/stable/scoped_access/

thanks it works now :slight_smile:

did you take a look at the plugin he wrote? probably much easier than
writing it all yourself.

ruby script/plugin install
http://wota.jp/svn/rails/plugins/branches/stable/scoped_access/