Because I'm very slow - trying to use console


#1

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


#2

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”


#3

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/


#4

Try @first_name or :params[‘client’][‘first_name’]

Bob S.
http://www.railtie.net/


#5

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


#6

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:inlist2’

Why?

Craig


#7

On 2/2/06, Craig W. 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


#8

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


#9

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.


#10

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

first_name = ?", first_name])
end


and it is gone too…

thanks

Craig