Building multiple find conditions

I am trying to allow for AND type ‘find’ but to allow for simply a
single set of find criteria.

controller code…

@vw_string = @vw_string1 = @vw_string2 = @vw_string3 = []

  if params[:beg_intake_date] != "" then
    @vw_string1 = ["intake_date between ? and ?",
    params[:beg_intake_date], params[:end_intake_date] ]
  end

  if params[:beg_referral_date] != "" then
    @vw_string2 = ["referral_date between ? and ?",
    params[:beg_referral_date], params[:end_referral_date] ]
  end

  if params[:beg_discharge_date] != "" then
    @vw_string3 = ["discharge_date between ? and ?",
    params[:beg_discharge_date], params[:end_discharge_date] ]
  end

-> @vw_string = [@vw_string1, @vw_string2, @vw_string3]

  @placement_pages, @placements = paginate(
    :placements,
    :conditions => @vw_string,
    :include => [:client, :case_manager, :facility],
    :order_by => 'referral_date',
    :per_page => 14)

The above setup gives me an error 'undefined method '%" for []:Array

Same thing if I set…

@vw_string = @vw_string1, @vw_string2, @vw_string3

and if I do it like this…

@vw_string = [@vw_string1] [@vw_string2] [@vw_string3]

I get can’t convert Array into Integer

How can I get the @vw_string into something that allows for ‘AND’
conditions or singular conditions if only 1 criteria resulted from a
search?

Craig

Craig-

You can do this quite easily with my ez_where plugin, get it from

svn here:

http://opensvn.csie.org/ezra/rails/plugins/dev/ez_where/

And here is an example of doing what you want to do with it, I am

assuming that your model is called Placement. Note that in my plugin
the shorthand for a BETWEEN ? AND ? query is <=> the spaceship
operator :wink: Also note that I am using the range operator … to make
the date ranges.

cond = Caboose::EZ::Condition.new do
intake_date <=> (params[:beg_intake_date]…params
[:end_intake_date]) unless params[:beg_intake_date].nil?
referral_date <=> [ params[:beg_referral_date]…params
[:end_referral_date] ] unless params[:beg_referral_date].nil?
discharge_date <=> [ params[:beg_discharge_date]…params
[:end_discharge_date] ] unless params[:beg_discharge_date].nil?
end

Then you can use cond.to_sql in your copnditions like:

:conditions => cond.to_sql

So if all three dates are available in the params hash your
cond.to_sql will look like this:

p cond.to_sql
=> [“intake_date BETWEEN ? AND ? AND referral_date BETWEEN ? AND ?
AND discharge_date BETWEEN ? AND ?”, Sun Feb 19 14:02:44 PST 2006,
Mon Feb 20 14:02:44 PST 2006, Sun Feb 19 14:02:44 PST 2006, Mon Feb
20 14:02:44 PST 2006, Sun Feb 19 14:02:44 PST 2006, Mon Feb 20
14:02:44 PST 2006]

If only some or one of the dates is present in params then it will do
the right thing and either just put the one date range in there or
use4 AND to join together as many date ranges as are present in the
params.

Here is a link to my blog where I write up all the other ways to use
my plugin:

http://brainspl.at/articles/2006/01/30/i-have-been-busy

Cheers-
-Ezra

On Feb 19, 2006, at 12:02 PM, Craig W. wrote:

  end

How can I get the @vw_string into something that allows for ‘AND’
conditions or singular conditions if only 1 criteria resulted from a
search?

Craig


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

-Ezra Z.
WebMaster
Yakima Herald-Republic Newspaper
[email protected]
509-577-7732

I think I just got a woodie…thanks - I am going to dive into this
later because I’ve got a client to please at the moment but it appears
to be a blessing of many magnitudes.

Thanks

Craig

Craig -

There was  slight typo in my last email. The setup should be thus:

cond = Caboose::EZ::Condition.new do
intake_date <=> (params[:beg_intake_date]…params
[:end_intake_date]) unless params[:beg_intake_date].nil?
referral_date <=> ( params[:beg_referral_date]…params
[:end_referral_date] ) unless params[:beg_referral_date].nil?
discharge_date <=> ( params[:beg_discharge_date]…params
[:end_discharge_date] ) unless params[:beg_discharge_date].nil?
end

Cheers-
-Ezra

On Feb 19, 2006, at 2:19 PM, Craig W. wrote:

14:02:44 PST 2006]

  end
    :conditions => @vw_string,

and if I do it like this…

509-577-7732


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

-Ezra Z.
WebMaster
Yakima Herald-Republic Newspaper
[email protected]
509-577-7732

That looks fine… but wouldn’t it be easier to just try it out first?
:wink:

On Apr 13, 2006, at 10:21 PM, Arch S. wrote:


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


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

@posts = Post.find_with_block(:all) do
published == 1
author == author
end

That should do it for you.

Cheers-
-Ezra

I want to search for two conditions in one find. Is there no way to do
this without the plugin?

example:

@posts = Post.find(:all, :conditions => [ “published = 1 AND author =
?”, author])

Is that legal/supported?

On Apr 14, 2006, at 8:59 AM, Guest wrote:

http://lists.rubyonrails.org/mailman/listinfo/rails
Cheers-
-Ezra

I don’t see that method in the api. Is the documentation
(http://api.rubyonrails.com/) out of date?

Guest-

No thats not part of core rails. It's my plugin called ez_where. It

adds a ton of functionality to create dynamic :conditions on your AR
find’s. You can read more about it here:

http://brainspl.at/articles/2006/01/30/i-have-been-busy

Cheers-
-Ezra

Ezra Z. wrote:

On Apr 13, 2006, at 10:21 PM, Arch S. wrote:


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


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

@posts = Post.find_with_block(:all) do
published == 1
author == author
end

That should do it for you.

Cheers-
-Ezra

I don’t see that method in the api. Is the documentation
(http://api.rubyonrails.com/) out of date?