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