# Adding condition filter to find records

**URL:** <https://www.ruby-forum.com/t/adding-condition-filter-to-find-records/67594>\
**Category:** Rails\
**Created:** [September 5, 2006, 2:53pm UTC](https://www.ruby-forum.com/t/adding-condition-filter-to-find-records/67594 "2006-09-05T14:53:35Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [September 5, 2006, 2:53pm UTC](https://www.ruby-forum.com/t/adding-condition-filter-to-find-records/67594/1 "2006-09-05T14:53:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [September 5, 2006, 3:54pm UTC](https://www.ruby-forum.com/t/adding-condition-filter-to-find-records/67594/2 "2006-09-05T15:54:45Z")

</div>

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.

> **[ActiveRecord::Base](https://api.rubyonrails.org/classes/ActiveRecord/Base.html#M000892)**
>
> Active Record Active Record objects don't specify their attributes directly, but rather infer them from the table definition with which they're linked.

[http://habtm.com/articles/2006/02/22/nested-with\_scope](http://habtm.com/articles/2006/02/22/nested-with_scope)

Chris

---

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [September 5, 2006, 7:19pm UTC](https://www.ruby-forum.com/t/adding-condition-filter-to-find-records/67594/3 "2006-09-05T19:19:11Z")

</div>

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.
> 
> [ActiveRecord::Base](http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M000892)  
> [http://habtm.com/articles/2006/02/22/nested-with\_scope](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

---

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [September 6, 2006, 11:19am UTC](https://www.ruby-forum.com/t/adding-condition-filter-to-find-records/67594/4 "2006-09-06T11:19:06Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [September 6, 2006, 11:22am UTC](https://www.ruby-forum.com/t/adding-condition-filter-to-find-records/67594/5 "2006-09-06T11:22:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [September 6, 2006, 5:12pm UTC](https://www.ruby-forum.com/t/adding-condition-filter-to-find-records/67594/6 "2006-09-06T17:12:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [September 5, 2006, 10:36pm UTC](https://www.ruby-forum.com/t/adding-condition-filter-to-find-records/67594/7 "2006-09-05T22:36:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [September 6, 2006, 5:28pm UTC](https://www.ruby-forum.com/t/adding-condition-filter-to-find-records/67594/8 "2006-09-06T17:28:21Z")

</div>

just put the ScopedAccess class in your models directory.

---

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [September 6, 2006, 6:42pm UTC](https://www.ruby-forum.com/t/adding-condition-filter-to-find-records/67594/9 "2006-09-06T18:42:32Z")

</div>

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:in`const\_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.

---

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [September 6, 2006, 6:51pm UTC](https://www.ruby-forum.com/t/adding-condition-filter-to-find-records/67594/10 "2006-09-06T18:51:38Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [September 6, 2006, 8:07pm UTC](https://www.ruby-forum.com/t/adding-condition-filter-to-find-records/67594/11 "2006-09-06T20:07:42Z")

</div>

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/](http://wota.jp/svn/rails/plugins/branches/stable/scoped_access/)

thanks it works now 🙂

---

<div class="post-metadata">

**Author:** ![nicken](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/d07c76/32.png) [@nicken](https://www.ruby-forum.com/u/nicken)\
**Post date:** [September 6, 2006, 7:48pm UTC](https://www.ruby-forum.com/t/adding-condition-filter-to-find-records/67594/12 "2006-09-06T19:48:43Z")

</div>

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/](http://wota.jp/svn/rails/plugins/branches/stable/scoped_access/)
