# Complicated finds are eating my sole

**URL:** <https://www.ruby-forum.com/t/complicated-finds-are-eating-my-sole/53550>\
**Category:** Rails\
**Created:** [February 9, 2006, 4:43am UTC](https://www.ruby-forum.com/t/complicated-finds-are-eating-my-sole/53550 "2006-02-09T04:43:48Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 9, 2006, 4:43am UTC](https://www.ruby-forum.com/t/complicated-finds-are-eating-my-sole/53550/1 "2006-02-09T04:43:48Z")

</div>

I abandoned ruby way for find\_by\_sql and still can’t get this…

@myplacement = Placement.find\_by\_sql(  
“select \* from placement where  
:intake\_date \>= beg\_intake\_date  
and  
:intake\_date \<= end\_intake\_date”)

just a simple date range…it’s killing me - If I knew how to load  
placement controller into irb, I could probably try out finds  
interactive mode…

Thanks

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 9, 2006, 4:53am UTC](https://www.ruby-forum.com/t/complicated-finds-are-eating-my-sole/53550/2 "2006-02-09T04:53:07Z")

</div>

Hi Craig,

What helps me when I have to resort to a find\_by\_sql for a hairy query  
is to  
set it up like:

sql = “select \*from placement where  
:intake\_date \>= beg\_intake\_date  
and  
:intake\_date \<= end\_intake\_date”)

breakpoint  
find\_by\_sql(sql)

With the breakpoint there I can see what my sql statement will look like  
once the variables are populated. Then I take that statement over to my  
database front-end client and run it so I can see if it works directly  
in  
my\_sql or whatever.

But I’m sure if you work at it, you can probably use a regular find with  
:conditions.

Steve  
[http://www.smarkets.net](http://www.smarkets.net)

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 9, 2006, 4:53am UTC](https://www.ruby-forum.com/t/complicated-finds-are-eating-my-sole/53550/3 "2006-02-09T04:53:42Z")

</div>

Try this:

@myplacement = Placement.find\_by\_sql(  
[“select \* from placement where intake\_date \>= ?  
and intake\_date \<= ?”, begin\_intake\_date, end\_intake\_date])

Assuming intake\_date is the name of your field and {beg|end}\_intake\_date  
are  
local variables or methods of your model.

Bob S.  
[http://www.railtie.net/](http://www.railtie.net/)

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 9, 2006, 5:07am UTC](https://www.ruby-forum.com/t/complicated-finds-are-eating-my-sole/53550/4 "2006-02-09T05:07:52Z")

</div>

On 2/8/06, Craig W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> interactive mode…  
> @placements = Placement.find :all, :conditions =\> [“intake\_date  
> between ? and ?”, begin\_date, end\_date]

…where begin\_date and end\_date are the values you’d like to populate  
the query with.  
If this is a controller action, they are probably in the params, etc,  
etc.

find :all and find\_by\_sql both return all matches, so remember that  
@placements is likely to be an Array of Placement instances.

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 9, 2006, 5:36am UTC](https://www.ruby-forum.com/t/complicated-finds-are-eating-my-sole/53550/5 "2006-02-09T05:36:48Z")

</div>

Hi Craig,  
if ur concern is only to look at the query which is fired then look into  
the log/development.log file. Break point is generally setup on the  
controller only & the way u have set it up seems to be correct, are u  
developing on ur local system or a remote machine( if remote then check  
the firewall settings) coz breakpointer uses a different port( i forgot  
the no.) & u should allow connections to that.

Suraj

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 9, 2006, 5:27am UTC](https://www.ruby-forum.com/t/complicated-finds-are-eating-my-sole/53550/6 "2006-02-09T05:27:23Z")

</div>

On Wed, 2006-02-08 at 21:52 -0600, Steve O. wrote:

> breakpoint  
> find\_by\_sql(sql)
> 
> With the breakpoint there I can see what my sql statement will look  
> like once the variables are populated. Then I take that statement over  
> to my database front-end client and run it so I can see if it works  
> directly in my\_sql or whatever.
> 
> But I’m sure if you work at it, you can probably use a regular find  
> with :conditions.

* * *

I couldn’t figure out how to do the ‘conditions’ with a date  
range…definitely couldn’t find anything like that in Agile book and  
even scoured through Pickaxe for a hint.

Now the breakpoint thing is something that would be worthwhile knowing.

I can’t seem to make that work. I am looking through that p195/196 and  
it suggests that I use the ‘method breakpoint()’

I have tried inserting…

sql = "select \* …  
breakpoint() #also breakpoint  
find\_by\_sql(sql)

in the controller and also tried in the rhtml page that I am loading -  
inserting it before the part where it errors and it blows right through  
the appropriate location in the controller method and in the rhtml page.

so I can’t make that work

I have a console open running breakpointer - waiting for the bus…but I  
guess the bus is at Disneyworld.

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 9, 2006, 6:04am UTC](https://www.ruby-forum.com/t/complicated-finds-are-eating-my-sole/53550/7 "2006-02-09T06:04:40Z")

</div>

I remember having some trouble figuring out the breakpoint thing… I  
don’t remember what.  
But it works for me now… and I just did one in a controller and one in  
an rhtml file  
(inside a \<% %\>)… both worked.

Here’s what I did…

I put “breakpoint” in an action inside a controller:

def list  
breakpoint  
@proposal\_pages, @proposals = paginate :proposals, :per\_page =\> 10  
end

and started webrick.

Then I opened a terminal window changed to my project directory and ran  
“ruby  
script\breakpointer”. It said something about it’d be checking for  
connections every 2  
seconds.

Then I opened a browser and invoked the controller with the breakpoint  
and the terminal  
window said something new (sorry, I forgot what) and gave me a “\>”. I  
typed “inspect” and  
got a ton of crap… I typed “methods” and got some more crap… I typed  
“@session.inspect” and got all the stuff in the session…

Hope that helps some how!

b

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 9, 2006, 7:26am UTC](https://www.ruby-forum.com/t/complicated-finds-are-eating-my-sole/53550/8 "2006-02-09T07:26:16Z")

</div>

On Wed, 2006-02-08 at 23:07 -0500, Wilson B. wrote:

> > placement controller into irb, I could probably try out finds  
> > @placements is likely to be an Array of Placement instances.

* * *

this has turned out to be a real bugger…and I’ve has some great advice  
from some very smart people (where’s Estelle when I need her? 😉

as you can see by the log…the data is there but it certainly is  
delivering a ‘null’ in the SQL Select command…

Processing PlacementsController#findresult (for 127.0.0.1 at 2006-02-08  
23:13:22) [POST]  
Parameters: {“commit”=\>“Find”, “action”=\>“findresult”,  
“controller”=\>“placements”, “placement”=\>{“beg\_referral\_date”=\>“”,  
“end\_referral\_date”=\>“”, “beg\_discharge\_date”=\>“”,  
“end\_intake\_date”=\>“01/01/2007”, “end\_discharge\_date”=\>“”,  
“beg\_intake\_date”=\>“01/01/2001”}}

(separation here for readability)

Placement Count (0.002219) SELECT COUNT(\*) FROM placements WHERE  
(intake\_date between NULL and NULL)  
Placement Load (0.009762) SELECT \* FROM placements WHERE  
(intake\_date between NULL and NULL) LIMIT 14 OFFSET 0  
Rendering within layouts/placements  
Rendering placements/findresult  
Completed in 0.05280 (18 reqs/sec) | Rendering: 0.00669 (12%) | DB:  
0.01198 (22%) | 200 OK [[http://localhost/placements/findresult](http://localhost/placements/findresult)]  
[craig@srv2 th-db]$

Is it because it rails doesn’t recognize “01/01/2001” as a date?

Select \* FROM placements WHERE (intake\_date between NULL and NULL)

in case it offers clues…this is how the section goes…

def findresult

```
beg_intake_date = params[:beg_intake_date]
end_intake_date = params[:end_intake_date]
beg_referral_date = params[:beg_referral_date]
end_referral_date = params[:end_referral_date]
beg_discharge_date = params[:beg_discharge_date]
end_discharge_date = params[:end_discharge_date]

if beg_intake_date != "" then
  @placement_pages, @placements = paginate(
    :placements,
    :conditions => ["intake_date between ? and ?",
      beg_intake_date, end_intake_date],
    :per_page => 14)
elsif beg_referral_date != "" then

```

# I’m sure that I don’t have to keep repeating the pattern here…

This has taken up the WHOLE evening - ouch

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 9, 2006, 7:32am UTC](https://www.ruby-forum.com/t/complicated-finds-are-eating-my-sole/53550/9 "2006-02-09T07:32:39Z")

</div>

On Wed, 2006-02-08 at 21:06 -0800, Ben M. wrote:

> ```
> @proposal_pages, @proposals = paginate :proposals, :per_page => 10
> 
> ```
> 
> got a ton of crap… I typed “methods” and got some more crap… I typed  
> “@session.inspect” and got all the stuff in the session…
> 
> Hope that helps some how!

* * *

interesting ‘technical’ description of the process…I _ **think** _ I  
understand. I have been focused on cleaning up the find by dates thing  
which seems so simple but is clearly beyond my reach. I do want to  
understand this though…

Thanks

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 9, 2006, 11:07am UTC](https://www.ruby-forum.com/t/complicated-finds-are-eating-my-sole/53550/10 "2006-02-09T11:07:58Z")

</div>

On 9 Feb 2006, at 08:38, Mikkel B. wrote:

> Try
> 
> (6.months.ago.to\_date…1.year.ago.to\_date).to\_s(:db)
> 
> Which produces:
> 
> =\> “BETWEEN ‘2005-07-28’ AND ‘2005-01-23’”
> 
> Courtesy of [http://www.jeremydurham.com/blog/post/6](http://www.jeremydurham.com/blog/post/6)

Brilliant!

Thanks Mikkel, and Jeremy.

Jon

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 9, 2006, 5:37pm UTC](https://www.ruby-forum.com/t/complicated-finds-are-eating-my-sole/53550/11 "2006-02-09T17:37:50Z")

</div>

On Thu, 2006-02-09 at 08:38 +0000, Mikkel B. wrote:

> Complicated finds should really be placed in the model…

* * *

that makes sense but I think that my problem is dealing with an existing  
string that looks like a date but isn’t and I’m getting strange results  
when trying to convert it…

NoMethodError in Placements#findresult  
You have a nil object when you didn’t expect it!  
The error occured while evaluating nil.to\_date

Request  
Parameters: {“commit”=\>“Find”, “placement”=\>{“beg\_referral\_date”=\>"",  
“end\_referral\_date”=\>"", “beg\_discharge\_date”=\>"",  
“end\_intake\_date”=\>“2007-01-01”, “end\_discharge\_date”=\>"",  
“beg\_intake\_date”=\>“2001-01-01”}}

def findresult

```
beg_in_date = params[:beg_intake_date]

beg_intake_date = beg_in_date.to_date # line that causes error

```

It seems that beg\_in\_date is null - even though you can see that the  
Parameters passed show a value in beg\_intake\_date

Why is this happening?

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 9, 2006, 5:44pm UTC](https://www.ruby-forum.com/t/complicated-finds-are-eating-my-sole/53550/12 "2006-02-09T17:44:16Z")

</div>

Craig W. wrote:

> Request  
> Parameters: {“commit”=\>“Find”, “placement”=\>{“beg\_referral\_date”=\>"",  
> “end\_referral\_date”=\>"", “beg\_discharge\_date”=\>"",  
> “end\_intake\_date”=\>“2007-01-01”, “end\_discharge\_date”=\>"",  
> “beg\_intake\_date”=\>“2001-01-01”}}
> 
> def findresult
> 
> ```
> beg_in_date = params[:beg_intake_date]
> 
> beg_intake_date = beg_in_date.to_date # line that causes error
> 
> ```
> 
> It seems that beg\_in\_date is null - even though you can see that the  
> Parameters passed show a value in beg\_intake\_date

Try this…

beg\_intake\_date = params[“placement”][“beg\_intake\_date”]

\_Kevin

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 9, 2006, 9:38am UTC](https://www.ruby-forum.com/t/complicated-finds-are-eating-my-sole/53550/13 "2006-02-09T09:38:49Z")

</div>

> * * *
> 
> interesting ‘technical’ description of the process…I _ **think** _ I  
> understand. I have been focused on cleaning up the find by dates thing  
> which seems so simple but is clearly beyond my reach. I do want to  
> understand this though…
> 
> Thanks
> 
> Craig

Complicated finds should really be placed in the model…

Try

(6.months.ago.to\_date…1.year.ago.to\_date).to\_s(:db)

Which produces:

=\> “BETWEEN ‘2005-07-28’ AND ‘2005-01-23’”

Courtesy of [http://www.jeremydurham.com/blog/post/6](http://www.jeremydurham.com/blog/post/6)

Mikkel B.

www.strongside.dk - Football Portal(DK)  
[nflfeed.helenius.org](http://nflfeed.helenius.org) - Football News(DK)  
ting.minline.dk - Buy Old Stuff!(DK)

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 9, 2006, 7:14pm UTC](https://www.ruby-forum.com/t/complicated-finds-are-eating-my-sole/53550/14 "2006-02-09T19:14:39Z")

</div>

Sorry to hear about your feet. Or is it your fish they’re eating? 😉

Best regards,  
Bill

----- Original Message -----  
From: “Craig W.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Sent: Wednesday, February 08, 2006 9:43 PM  
Subject: [Rails] complicated finds are eating my sole

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 9, 2006, 5:58pm UTC](https://www.ruby-forum.com/t/complicated-finds-are-eating-my-sole/53550/15 "2006-02-09T17:58:48Z")

</div>

On Thu, 2006-02-09 at 17:44 +0100, Kevin O. wrote:

> > ```
> > beg_intake_date = beg_in_date.to_date # line that causes error
> > 
> > ```
> > 
> > It seems that beg\_in\_date is null - even though you can see that the  
> > Parameters passed show a value in beg\_intake\_date
> 
> Try this…
> 
> beg\_intake\_date = params[“placement”][“beg\_intake\_date”]

* * *

awesome - thanks

Craig

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 9, 2006, 8:38pm UTC](https://www.ruby-forum.com/t/complicated-finds-are-eating-my-sole/53550/16 "2006-02-09T20:38:37Z")

</div>

Just another demonstration of my capacity to put my foot in my mouth.

Obviously I was so flabbergasted by then, homonyms/malapropisms were the  
least of my issues - thanks to the list are now resolved - notice - no  
questions for hours now…

😉

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 9, 2006, 7:39pm UTC](https://www.ruby-forum.com/t/complicated-finds-are-eating-my-sole/53550/17 "2006-02-09T19:39:44Z")

</div>

|Hi,

You can do ranges with this nice little rails date helper  
#Date Range  
(Date.today…1.year.ago.to\_date).to\_s(:db) #=\> “BETWEEN ‘2006-01-24’ AND  
‘2005-01-24’”

So that leave us with:

between = ||(Date.today…1.year.ago.to\_date).to\_s(:db)  
intake\_date = intake\_date.to\_s(:db)

|@myplacement = Placement.|find\_by\_sql(“SELECT \* from placement WHERE #{||intake\_date||} #{||between}”||)

I haven’t tested this specific code, but it should work.

Eric

|

> > removed\_email\_address@domain.invalid  
> > [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)
> 
> * * *
> 
> Rails mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

–  
Eric G.  
[http://www.ericgoodwin.com](http://www.ericgoodwin.com)

---

<div class="post-metadata">

**Author:** ![craigrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/c2a13f/32.png) [@craigrails](https://www.ruby-forum.com/u/craigrails)\
**Post date:** [February 9, 2006, 8:43pm UTC](https://www.ruby-forum.com/t/complicated-finds-are-eating-my-sole/53550/18 "2006-02-09T20:43:10Z")

</div>

Thanks…one of the problems with ‘find\_by\_sql’ method is invalid  
searches give no warning to me where ruby methods do - I actually got a  
working ruby method and in the process…I have learned A LOT and now am  
much clearer on how things work in rails.

It took some time and some really bad hacks to get me this far but now I  
am going back and cleaning up some of the stupid stuff I did that worked  
in favor of clean stuff because I now have a better grip on things and  
can keep things a bit more ‘DRY’

Obviously one of the things that always throws me for a loop is when to  
use [] () and {} or simply not to use them at all…which has a  
profound effect on anything in my controllers related to searches.

One thing that I get now…  
I really like \<% render :partial … %\>  
That has really simplified things a lot

Craig
