Using conditions appropriately


#1

I’ve been struggling with this function for most of the morning, but
something just doesn’t seem right in my logic with respect to
efficiency.

I have a basic view that is submitting information on three variables
directly to this function. The information is accessible simply through
params[:filtera1], params[:filtera2] and params[:filtera1].

I’m trying to build a find that will check the filter_a column in my
database table (places) and list the record if filter_a matches any one
of the three parameters just passed to it.

The difficulty comes from two areas

  1. I have not perfected the right syntax for using OR in conditions and
    most of the documentation I have found is not too helpful.

  2. There is the chance that some of the information passed through may
    be equal to nil - and finds like to break at that point.

Any help would be appreciated.

PS: If anyone is looking for a small job of helping me figure out these
basic concepts in exchange for some weekly drinking money, I’d be more
than happy to consider such an arrangement. Everyone’s time is valuable,
afterall.


#2

Why not post your code so we can take a look at the logic?

Regards,
Dave


Information and Educational Technology
Kwantlen University College - 604-599-2120

Robert S. removed_email_address@domain.invalid
Sent by: removed_email_address@domain.invalid
05-07-2007 12:50 PM
Please respond to
removed_email_address@domain.invalid

To
removed_email_address@domain.invalid
cc

Subject
[Rails] Using conditions appropriately

I’ve been struggling with this function for most of the morning, but
something just doesn’t seem right in my logic with respect to
efficiency.

I have a basic view that is submitting information on three variables
directly to this function. The information is accessible simply through
params[:filtera1], params[:filtera2] and params[:filtera1].

I’m trying to build a find that will check the filter_a column in my
database table (places) and list the record if filter_a matches any one
of the three parameters just passed to it.

The difficulty comes from two areas

  1. I have not perfected the right syntax for using OR in conditions and
    most of the documentation I have found is not too helpful.

  2. There is the chance that some of the information passed through may
    be equal to nil - and finds like to break at that point.

Any help would be appreciated.

PS: If anyone is looking for a small job of helping me figure out these
basic concepts in exchange for some weekly drinking money, I’d be more
than happy to consider such an arrangement. Everyone’s time is valuable,
afterall.


Posted via http://www.ruby-forum.com/.


#3

On 7/5/07, Robert S. removed_email_address@domain.invalid wrote:

the basic tests, this works fine and passes the data along to the
%>
params[:filtera1] OR :filter_a => params[:filtera2] OR :filter_a =>
params[:filtera3]})

end

use something like the following:

def results
conditions = “true”
par = []

unless params[:filtera1].blank?
  conditions << " and filtera = ?"
  par << params[:filtera1]
end

unless params[:filtera2].blank?
  conditions << " or filtera = ?"
  par << params[:filtera2]
end

unless params[:filtera3].blank?
  conditions << " or filtera = ?"
  par << params[:filtera3]
end

@results = Place.find(:all, :conditions => [conditions] + par)

end

or use a plugin such as criteria_query
(http://www.muermann.org/ruby/criteria_query/)

Mike


#4

Table: Places
name - location name
filtera - integer corresponding to a particular area (1, 2 or 3)

View:
Allows user to select what area they are located in. Checkboxes and Ajax
were utilized to make the results table dynamically update as they make
selections, and also to enable users to make multiple selections. From
the basic tests, this works fine and passes the data along to the
results function.

<%= observe_form 'interest', :frequency => 0.25, :update => 'search_target', :url => { :controller => 'stores', :action => 'results' } %>

Controller:

def results

@results = Place.find(:all, :conditions => { :filter_a =>
params[:filtera1] OR :filter_a => params[:filtera2] OR :filter_a =>
params[:filtera3]})

end