# How to stop all records from being returned

**URL:** <https://www.ruby-forum.com/t/how-to-stop-all-records-from-being-returned/72201>\
**Category:** Rails\
**Created:** [October 15, 2006, 2:12pm UTC](https://www.ruby-forum.com/t/how-to-stop-all-records-from-being-returned/72201 "2006-10-15T14:12:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 15, 2006, 2:12pm UTC](https://www.ruby-forum.com/t/how-to-stop-all-records-from-being-returned/72201/1 "2006-10-15T14:12:07Z")

</div>

Trying various things here to eliminate this problem. In my dyanmic  
search ,  
if no search criteria is selected then all records are returned. It’s  
not  
what I want. By the way this is an observe\_form. Help is greatly  
appreciated.

Here is the controller -

def list  
category\_id = params[:category\_id]  
state\_id = params[:state\_id]  
term\_id = params[:term\_id]  
city = params[:city]  
title = params[:title]

```
    pq = Position.query # I'm using the plugin CriteriaQuery
    pq.category_id_in(params[:category_id])

```

unless(params[:category\_id]|| “”).empty?  
pq.state\_id\_in(params[:state\_id]) unless(params[:state\_id]||  
“”).empty?  
pq.term\_id\_in(params[:term\_id]) unless(params[:term\_id]||  
“”).empty?

```
    pq.city_in(params[:city].split(",")) unless(params[:city]||

```

“”).empty?  
pq.title\_in(params[:title].split(“,”)) unless(params[:title]||  
“”).empty?  
pp pq

```
    @positions = pq.find
    render :partial => "positions_list", :layout => false

 end

```

end

Here is the observe\_form:# in the possibility that I can somehow enforce  
it  
here to not return any records in the event nothing is selected  
# I’m also showing my partial down  
below

Category \<%= options\_from\_collection\_for\_select @categories, :id, :name, :prompt =\> true %\> State \<%= options\_from\_collection\_for\_select @states, :id, :name %\> Terms \<%= options\_from\_collection\_for\_select @terms, :id, :name %\> Title City \<%= image\_tag("roller.gif", :align =\> 'center', :border =\> 0, :id =\> "roller", :style =\> "display: none;") %\>

\<%= observe\_form “asearch”,  
:frequency =\> 0.5,  
:update =\> ‘table’,  
:before =\> “Element.show(‘roller’)”,  
:success =\> “Element.hide(‘roller’)”,  
:url =\> ‘list’ %\>

Partial:  
\<% if @positions == nil %\>

# \<%="Enter search criteria"%\>
\<% elsif @positions.size == 0 %\>
# \<%="No jobs found using current search criteria"%\>
\<% else %\> \<% @positions.each do |p| %\> "\> \<% end %\> 

| Title | Category | Location | Terms |
| \<%= p.title %\> | \<%= p.category.name %\> | \<%= p.city %\>-\<%= p.state.name %\> | \<%= p.term.name %\> |

\<% end %\>

–

> **[Dark ambient](https://en.wikipedia.org/wiki/Dark_ambient)**
>
> Dark ambient (referred to as ambient industrial especially in the 1980s) is a genre of post-industrial music that features an ominous, dark droning and often gloomy, monumental or catacombal atmosphere, partially with discordant overtones. It shows similarities with ambient music, a genre that has been cited as a main influence by many dark ambient artists, both conceptually and compositionally. Although mostly electronically generated, dark ambient also includes the sampling of hand-pla 
> The te...

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 15, 2006, 2:26pm UTC](https://www.ruby-forum.com/t/how-to-stop-all-records-from-being-returned/72201/2 "2006-10-15T14:26:09Z")

</div>

Hi –

On Sun, 15 Oct 2006, Dark A. wrote:

> ```
> term_id = params[:term_id]
> city = params[:city]
> title = params[:title]
> 
> ```

It doesn’t look like you ever use those assignments. Are they  
necessary?

> ```
> pq = Position.query # I'm using the plugin CriteriaQuery
> 
> ```

I’m afraid I’m not familiar with it. However…

> ```
> pq.category_id_in(params[:category_id])
> 
> ```
> 
> unless(params[:category\_id]|| “”).empty?

If you think it might be nil or “”, you can use the blank? method.

> ```
> pq.state_id_in(params[:state_id]) unless(params[:state_id]||
> 
> ```
> 
> “”).empty?  
> pq.term\_id\_in(params[:term\_id]) unless(params[:term\_id]|| “”).empty?
> 
> ```
> pq.city_in(params[:city].split(",")) unless(params[:city]||
> 
> ```
> 
> “”).empty?  
> pq.title\_in(params[:title].split(“,”)) unless(params[:title]||  
> “”).empty?  
> pp pq

What does this show you?

David

–  
David A. Black | removed\_email\_address@domain.invalid  
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
[1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
[2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 15, 2006, 2:34pm UTC](https://www.ruby-forum.com/t/how-to-stop-all-records-from-being-returned/72201/3 "2006-10-15T14:34:11Z")

</div>

On 10/15/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > what I want. By the way this is an observe\_form. Help is greatly
> 
> It doesn’t look like you ever use those assignments. Are they  
> necessary?

No they do not get used, so no they are not necessary. However this is  
something I haven’t quite figured out yet regarding the plugin’s  
behaviour.  
Using regular AR syntax and find commands, they will recognize those  
assignment. For some reason I am finding CQ does not.

> ```
> pq = Position.query # I'm using the plugin CriteriaQuery
> 
> ```
> 
> I’m afraid I’m not familiar with it. However…
> 
> > ```
> > pq.category_id_in(params[:category_id])
> > 
> > ```
> > 
> > unless(params[:category\_id]|| “”).empty?
> 
> If you think it might be nil or “”, you can use the blank? method.

I have tried blank? with no success. Down for futher explanation.

> What does this show you?

This is the only way I could find to allow fields that were nil (user  
did  
not make selection) to be evaluated as “”.  
A fake out in other words.

Stuart

David

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 15, 2006, 3:02pm UTC](https://www.ruby-forum.com/t/how-to-stop-all-records-from-being-returned/72201/4 "2006-10-15T15:02:17Z")

</div>

Hi –

On Sun, 15 Oct 2006, Dark A. wrote:

> > > > ```
> > > > category_id = params[:category_id]
> > > > 
> > > > ```
> > 
> > something I haven’t quite figured out yet regarding the plugin’s behaviour.  
> > Using regular AR syntax and find commands, they will recognize those  
> > assignment. For some reason I am finding CQ does not.

I’m afraid I’m not following. It’s not up to CQ to recognize the  
assignments; it’s up to Ruby.

> > I have tried blank? with no success. Down for futher explanation.
> > 
> > > > ```
> > > > pp pq
> > > > 
> > > > ```
> > > 
> > > What does this show you?
> > 
> > This is the only way I could find to allow fields that were nil (user did  
> > not make selection) to be evaluated as “”.  
> > A fake out in other words.

If it’s absolutely necessary, then:

params[:city].to\_s.empty?

is a little nicer than that || thing.

> @restrictions=[],  
> #\<Criteria::Query:0xb43af50  
> @join\_aliases={“positions”=\>“positions”},  
> @model\_class=Position,  
> @restrictions=[]\>

I don’t have time right now to study CriteriaQuery, but it seems like  
a lot of work to do what should be a fairly simple query. Can you  
just use find with :conditions?

David

–  
David A. Black | removed\_email\_address@domain.invalid  
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
[1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
[2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 15, 2006, 3:10pm UTC](https://www.ruby-forum.com/t/how-to-stop-all-records-from-being-returned/72201/5 "2006-10-15T15:10:20Z")

</div>

On 10/15/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > On 10/15/06, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> > > 
> > > > > what I want. By the way this is an observe\_form. Help is greatly
> 
> I’m afraid I’m not following. It’s not up to CQ to recognize the
> 
> > > > > ```
> > > > > pq.title_in(params[:title].split(",")) unless(params[:title]||
> > > > > 
> > > > > ```
> 
> > @restrictions=  
> > @parent=#\<Criteria::Query:0xb5487b8 …\>,  
> > I don’t have time right now to study CriteriaQuery, but it seems like  
> > a lot of work to do what should be a fairly simple query. Can you  
> > just use find with :conditions?
> 
> David

I could use find with :conditions, although I haven’t really read up on  
how  
I could create a dynamic query of this nature.  
i.e. where any / mulitple or all fields could be used. I’m sure it can  
be  
done. CQ appealed to me as it seems a simpler way to write things. I  
wouldn’t be the best representative of the plugin.

Stuart

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 15, 2006, 2:50pm UTC](https://www.ruby-forum.com/t/how-to-stop-all-records-from-being-returned/72201/6 "2006-10-15T14:50:14Z")

</div>

Adding something at the bottom:

On 10/15/06, Dark A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > if no search criteria is selected then all records are returned. It’s  
> > > city = params[:city]
> 
> I have tried blank? with no success. Down for futher explanation.
> 
> > > ```
> > > pp pq
> > > 
> > > ```
> 
> David
> 
> >

Using PP , if for example 2 fields are selected:  
#\<Criteria::Query:0xb5487b8  
@join\_aliases={“positions”=\>“positions”},  
@model\_class=Position,  
@restrictions=  
[#\<Criteria::In:0xb5481a0  
@attribute\_name=“category\_id”,  
@model\_class=Position,  
@parent=#\<Criteria::Query:0xb5487b8 …\>,  
@query=#\<Criteria::Query:0xb5487b8 …\>,  
@restrictions=[],  
@value=[“1”]\>,  
#\<Criteria::In:0xb548098  
@attribute\_name=“state\_id”,  
@model\_class=Position,  
@parent=#\<Criteria::Query:0xb5487b8 …\>,  
@query=#\<Criteria::Query:0xb5487b8 …\>,  
@restrictions=[],  
@value=[“27”]\>]\>

However when all fields are deselected-  
#\<Criteria::Query:0xb43af50  
@join\_aliases={“positions”=\>“positions”},  
@model\_class=Position,  
@restrictions=[]\>

Stuart

–

> **[Dark ambient](https://en.wikipedia.org/wiki/Dark_ambient)**
>
> Dark ambient (referred to as ambient industrial especially in the 1980s) is a genre of post-industrial music that features an ominous, dark droning and often gloomy, monumental or catacombal atmosphere, partially with discordant overtones. It shows similarities with ambient music, a genre that has been cited as a main influence by many dark ambient artists, both conceptually and compositionally. Although mostly electronically generated, dark ambient also includes the sampling of hand-pla 
> The te...

---

<div class="post-metadata">

**Author:** ![zanoni](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/e99b99/32.png) [@zanoni](https://www.ruby-forum.com/u/zanoni)\
**Post date:** [October 15, 2006, 4:28pm UTC](https://www.ruby-forum.com/t/how-to-stop-all-records-from-being-returned/72201/7 "2006-10-15T16:28:29Z")

</div>

Top posting, because I solved the issue with -

if  
!params[:category\_id]  
!params[:state\_id]  
!params[:term\_id]  
!params[:city]  
!params[:title]  
“no criteria enetered”

```
    else

```

…  
…

So using the plugin Criteria Query, my code for the 5 form fields ( just  
posting here to see if using just find woudl require more/less or the  
same  
amount of code) -

pq = Position.query  
pq.category\_id\_in(params[:category\_id])  
unless(params[:category\_id]|| “”).empty?  
pq.state\_id\_in(params[:state\_id]) unless(params[:state\_id]||  
“”).empty?  
pq.term\_id\_in(params[:term\_id]) unless(params[:term\_id]||  
“”).empty?

```
    pq.city_in(params[:city].split(",")) unless(params[:city]||

```

“”).empty?  
pq.title\_in(params[:title].split(“,”)) unless(params[:title]||  
“”).empty?  
pp pq

```
    if
    !params[:category_id]
    !params[:state_id]
    !params[:term_id]
    !params[:city]
    !params[:title]
    "no criteria enetered"

    else
    @positions = pq.find
    render :partial => "positions_list", :layout => false

```

* * *

Stuart  
On 10/15/06, Dark A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > On 10/15/06, Dark A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> > > 
> > > > > > if no search criteria is selected then all records are returned.  
> > > > > > term\_id = params[:term\_id]  
> > > > > > behaviour.  
> > > > > > pq.category\_id\_in(params[:category\_id])  
> > > > > > “”).empty?
> > 
> > > @restrictions=[],  
> > > #\<Criteria::Query:0xb43af50
> 
> I could use find with :conditions, although I haven’t really read up on  
> how I could create a dynamic query of this nature.  
> i.e. where any / mulitple or all fields could be used. I’m sure it can be  
> done. CQ appealed to me as it seems a simpler way to write things. I  
> wouldn’t be the best representative of the plugin.
> 
> Stuart

–

> **[Dark ambient](https://en.wikipedia.org/wiki/Dark_ambient)**
>
> Dark ambient (referred to as ambient industrial especially in the 1980s) is a genre of post-industrial music that features an ominous, dark droning and often gloomy, monumental or catacombal atmosphere, partially with discordant overtones. It shows similarities with ambient music, a genre that has been cited as a main influence by many dark ambient artists, both conceptually and compositionally. Although mostly electronically generated, dark ambient also includes the sampling of hand-pla 
> The te...
