# Because I'm very slow - trying to use console

**URL:** <https://www.ruby-forum.com/t/because-im-very-slow-trying-to-use-console/100560>\
**Category:** Rails\
**Created:** [February 3, 2006, 6:54am UTC](https://www.ruby-forum.com/t/because-im-very-slow-trying-to-use-console/100560 "2006-02-03T06:54:26Z")\
**Posts on this page:** 10\
**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 3, 2006, 6:54am UTC](https://www.ruby-forum.com/t/because-im-very-slow-trying-to-use-console/100560/1 "2006-02-03T06:54:26Z")

</div>

I can’t see how to use variables so I am using console to test things  
out…

clients table - a column named first\_name

My very brief console session…

> > clients = Client.find\_by\_sql(“select \* from clients where first\_name  
> > = FN”)

ActiveRecord::StatementInvalid: RuntimeError: ERROR C42703 Mcolumn  
“fn” does not exist Fparse\_expr.c L1034 RtransformColumnRef:  
select \* from clients where first\_name = FN

from  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active\_record/connection\_adapters/abstract\_adapter.rb:88:in  
`log’

and a few lines of application trace removed.

How do I execute the line with the variable?

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 3, 2006, 6:57am UTC](https://www.ruby-forum.com/t/because-im-very-slow-trying-to-use-console/100560/2 "2006-02-03T06:57:26Z")

</div>

oops…left out an important line…

On Thu, 2006-02-02 at 22:51 -0700, Craig W. wrote:

> I can’t see how to use variables so I am using console to test things  
> out…
> 
> clients table - a column named first\_name
> 
> My very brief console session…

* * *

> > FN = “Elliot”  
> > =\> “Elliot”

–

---

<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 3, 2006, 7:03am UTC](https://www.ruby-forum.com/t/because-im-very-slow-trying-to-use-console/100560/3 "2006-02-03T07:03:28Z")

</div>

Quick easy way is to wrap FN in your query with #{FN}

Not sure if find\_by\_sql works with params but try this as well:

Client.find\_by\_sql([“select \* from client where first\_name = ?”, FN])

Good luck

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 3, 2006, 7:57am UTC](https://www.ruby-forum.com/t/because-im-very-slow-trying-to-use-console/100560/4 "2006-02-03T07:57:48Z")

</div>

Try @first\_name or :params[‘client’][‘first\_name’]

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 3, 2006, 8:12am UTC](https://www.ruby-forum.com/t/because-im-very-slow-trying-to-use-console/100560/5 "2006-02-03T08:12:51Z")

</div>

If your reference is using the rails way instead of the find\_by\_sql way,  
I haven’t gotten that to work yet.

If your reference is to change my find\_by\_sql statement, I got the same  
response from rails…‘Couldn’t find Client without an ID’ from both of  
your suggestions.

I have tried to reduce my confusion to the smallest possible elements  
and thought that the using ruby console would clarify things for me but  
when I can get command to run in console and take the same code  
(literally copied) into controller from console and it doesn’t work…It  
doesn’t help clarify things for me at all.

I wish I understood more fully…I have gone through the Agile book and  
have gotten through about 6 1/2 chapters of Pickaxe book…I’m trying.

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 3, 2006, 7:45am UTC](https://www.ruby-forum.com/t/because-im-very-slow-trying-to-use-console/100560/6 "2006-02-03T07:45:45Z")

</div>

On Thu, 2006-02-02 at 21:59 -0800, Bob S. wrote:

> Quick easy way is to wrap FN in your query with #{FN}
> 
> Not sure if find\_by\_sql works with params but try this as well:
> 
> Client.find\_by\_sql([“select \* from client where first\_name = ?”, FN])

* * *

indeed that works…I can’t understand why I can’t duplicate what works  
in console in rails code…

Console…

> > first\_name = “Elliot”  
> > =\> “Elliot”
> 
> > @clients = Client.find\_by\_sql([“select \* from clients where  
> > first\_name = ?”, first\_name])  
> > =\> [#\<Client:0xb791f2e8 @attributes={“dob”=\>“1920-01-30”,  
> > “gov\_id”=\>“333”, “id”=\>“33”, “case\_manager\_id”=\>“15”,  
> > “middle\_initial”=\>"", “first\_name”=\>“Elliot”,  
> > “last\_name”=\>“Templeton”}\>]

The Rails code…

find.rhtml #submits ‘first\_name’ (Elliot) to method ‘list2’

\<%= start\_form\_tag :action =\> ‘list2’ %\>  
\<%# render :partial =\> ‘form’ %\>

First name  
 \<%= text\_field 'client', 'first\_name' %\>

 \<%= submit\_tag 'Find' %\> \<%= end\_form\_tag %\>

clients\_controller.rb #has method ‘list2’  
def list2  
@client = Client.find(params[:id])  
@myclients = Client.find\_by\_sql([“select \* from clients where  
first\_name = ?”, first\_name])  
end

list2.rhtml # generates error when run…  
\<%# start\_form\_tag :action =\> ‘list2’, :id =\> @clients %\>

\<% odd\_or\_even = 0  
for client in @myclients  
odd\_or\_even = 1 - odd\_or\_even %\>

 \<%=h (client.wholename) %\>

and the error I get…

ActiveRecord::RecordNotFound in Clients#list2  
Couldn’t find Client without an ID

RAILS\_ROOT: script/…/config/…

Application Trace | Framework Trace | Full Trace  
/usr/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active\_record/base.rb:407:in  
`find' ./script/../config/../app/controllers/clients_controller.rb:13:in`list2’

Why?

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 3, 2006, 11:19am UTC](https://www.ruby-forum.com/t/because-im-very-slow-trying-to-use-console/100560/7 "2006-02-03T11:19:38Z")

</div>

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

> The Rails code…  
> clients\_controller.rb #has method ‘list2’  
> def list2  
> @client = Client.find(params[:id])

First, make sure that id is in params. I don’t think it is. Since  
it’s not, you can’t search for a client with the id, since you have no  
id. So, you’re searching for clients by their first name, so you want  
to get the first name from the params.

&ou want:  
first\_name = params[:client][:first\_name]

> ```
> @myclients = Client.find_by_sql(["select * from clients where
> 
> ```
> 
> first\_name = ?", first\_name])

You can probably rewrite the above line to  
@myclients = Client.find\_all\_by\_first\_name(first\_name)

> ```
> <td><%=h (client.wholename) %></td>
> 
> ```
> 
> and the error I get…
> 
> ActiveRecord::RecordNotFound in Clients#list2  
> Couldn’t find Client without an ID

You get this because params[:id] in your list2 method is nil. So  
yo’re trying to find a client with no id which breaks all kinds of  
stuff.

I’m really tired, so probably everything I said is wrong, sorry.

Joe

---

<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 3, 2006, 3:42pm UTC](https://www.ruby-forum.com/t/because-im-very-slow-trying-to-use-console/100560/8 "2006-02-03T15:42:17Z")

</div>

On Fri, 2006-02-03 at 02:16 -0800, Joe Van D. wrote:

> > in console in rails code…
> 
> first\_name = params[:client][:first\_name]
> 
> > \<%# start\_form\_tag :action =\> ‘list2’, :id =\> @clients %\>  
> > ActiveRecord::RecordNotFound in Clients#list2  
> > Couldn’t find Client without an ID
> 
> You get this because params[:id] in your list2 method is nil. So  
> yo’re trying to find a client with no id which breaks all kinds of  
> stuff.
> 
> I’m really tired, so probably everything I said is wrong, sorry.

* * *

No - it was just the last bit of info that I needed - it worked. Now I  
have to get it through my thick head why it works so I can utilize the  
methodology in the future.

Thanks Joe/Bob - you’ve gotten me over a hump that had me for days.

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 3, 2006, 6:28pm UTC](https://www.ruby-forum.com/t/because-im-very-slow-trying-to-use-console/100560/9 "2006-02-03T18:28:23Z")

</div>

It looks like the line of code that’s choking is the first one:

@client = client.find(params[:id])

Look at the sql in your development.log. It should shed some light.

---

<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 4, 2006, 12:43am UTC](https://www.ruby-forum.com/t/because-im-very-slow-trying-to-use-console/100560/10 "2006-02-04T00:43:58Z")

</div>

On Fri, 2006-02-03 at 09:26 -0800, Steve R. wrote:

> > first\_name = ?", first\_name])  
> > end

* * *

and it is gone too…

thanks

Craig
