Forum: Ruby on Rails Because I'm very slow - trying to use console

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-03 06:54
(Received via mailing list)
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
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-02-03 06:57
(Received via mailing list)
oops...left out an important line...

On Thu, 2006-02-02 at 22:51 -0700, Craig White 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"

--
Af93ba6b6b59f22a8f37e8de5702ef98?d=identicon&s=25 Bob Silva (Guest)
on 2006-02-03 07:03
(Received via mailing list)
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 Silva
http://www.railtie.net/
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-02-03 07:45
(Received via mailing list)
On Thu, 2006-02-02 at 21:59 -0800, Bob Silva 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' %>
  <p><label for="client_first_name">First name</label><br/>
  <%= text_field 'client', 'first_name'  %></p>
  <%= 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 %>

  <tr valign="top" class="ListLine<%= odd_or_even %>">
    <td><%=h (client.wholename) %></td>

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
Af93ba6b6b59f22a8f37e8de5702ef98?d=identicon&s=25 Bob Silva (Guest)
on 2006-02-03 07:57
(Received via mailing list)
Try @first_name or :params['client']['first_name']

Bob Silva
http://www.railtie.net/
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-02-03 08:12
(Received via mailing list)
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
38a8230ed3d5c685558b4f0aad3fc74b?d=identicon&s=25 Joe Van Dyk (Guest)
on 2006-02-03 11:19
(Received via mailing list)
On 2/2/06, Craig White <craigwhite@azapple.com> 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
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-02-03 15:42
(Received via mailing list)
On Fri, 2006-02-03 at 02:16 -0800, Joe Van Dyk 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
7e5be8d0d2537a6f6f00aada5541881e?d=identicon&s=25 Steve Ross (Guest)
on 2006-02-03 18:28
(Received via mailing list)
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.
C1e5a9e9344b6d31b9df7303e6dc378a?d=identicon&s=25 Craig White (Guest)
on 2006-02-04 00:43
(Received via mailing list)
On Fri, 2006-02-03 at 09:26 -0800, Steve Ross wrote:
> > first_name = ?", first_name])
> >   end
> >
----
and it is gone too...

thanks

Craig
This topic is locked and can not be replied to.