How to stop all records from being returned

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 %>

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 | [email protected]
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 | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

On 10/15/06, [email protected] [email protected] 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

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 | [email protected]
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 | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org

On 10/15/06, [email protected] [email protected] wrote:

On 10/15/06, [email protected] [email protected] 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

Adding something at the bottom:

On 10/15/06, Dark A. [email protected] 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

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. [email protected] wrote:

On 10/15/06, Dark A. [email protected] 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