Forum: Ruby on Rails building multiple find conditions

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.
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-02-19 21:04
(Received via mailing list)
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
8e44c65ac5b896da534ef2440121c953?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2006-02-19 23:08
(Received via mailing list)
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 ;-) 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 White 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
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>

-Ezra Zygmuntowicz
WebMaster
Yakima Herald-Republic Newspaper
ezra@yakima-herald.com
509-577-7732
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-02-19 23:20
(Received via mailing list)
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
8e44c65ac5b896da534ef2440121c953?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2006-02-19 23:27
(Received via mailing list)
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 White wrote:

>>
>>
>>
>> 14:02:44 PST 2006]
>>
>>>
>>>       end
>>>         :conditions => @vw_string,
>>> and if I do it like this...
>>>
>> 509-577-7732
>> _______________________________________________
>> Rails mailing list
>> Rails@lists.rubyonrails.org
>> http://lists.rubyonrails.org/mailman/listinfo/rails
>
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>

-Ezra Zygmuntowicz
WebMaster
Yakima Herald-Republic Newspaper
ezra@yakima-herald.com
509-577-7732
A111108ed3617edc35da89e84689c3c2?d=identicon&s=25 Arch Stanton (d6veteran)
on 2006-04-14 07:21
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?
43ec5998360a0cc21b51afef1781e9c0?d=identicon&s=25 Daniel Higginbotham (Guest)
on 2006-04-14 08:25
(Received via mailing list)
That looks fine... but wouldn't it be easier to just try it out first?
;)
6076c22b65b36f5d75c30bdcfb2fda85?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2006-04-14 16:44
(Received via mailing list)
On Apr 13, 2006, at 10:21 PM, Arch Stanton wrote:

>
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> 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
Ba3fa8291d2a20689528eab51ad780af?d=identicon&s=25 Guest (Guest)
on 2006-04-14 17:59
Ezra Zygmuntowicz wrote:
> On Apr 13, 2006, at 10:21 PM, Arch Stanton wrote:
>
>>
>>
>> --
>> Posted via http://www.ruby-forum.com/.
>> _______________________________________________
>> Rails mailing list
>> Rails@lists.rubyonrails.org
>> 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?
6076c22b65b36f5d75c30bdcfb2fda85?d=identicon&s=25 Ezra Zygmuntowicz (Guest)
on 2006-04-14 18:49
(Received via mailing list)
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
This topic is locked and can not be replied to.