Forum: Ruby on Rails Using conditions appropriately

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Robert S. (Guest)
on 2007-07-05 23:50
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.
David D. (Guest)
on 2007-07-06 00:05
(Received via mailing list)
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/.
Robert S. (Guest)
on 2007-07-06 00:16
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.

<form name="interest" id="interest">
<input name="filtera1" type="checkbox" id="filtera1" value="1" />
<input name="filtera2" type="checkbox" id="filtera2" value="2" />
<input name="filtera3" type="checkbox" id="filtera3" value="3" />
</form>
<%= observe_form 'interest',   :frequency => 0.25,
              :update => 'search_target',
              :url => { :controller => 'stores', :action => 'results' }
%>

<div id="search_target">

</div>

Controller:

def results

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


end
Mike G. (Guest)
on 2007-07-06 00:50
(Received via mailing list)
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
This topic is locked and can not be replied to.