Hi, I have some places where I have to issue a query to retrieve records (which differ for each issued query as the query is dynamically generated), but I still want to be able to use the result the activerecord way. I had defined a class Result I exclusively used as this: Result.find_by_sql(blah blah) There''s no result table in my database, and it worked fine until I upgrade to the latest rails (I still used an older version, 0.12?). Now it seems the table has to exist in the database. So, what''s my best solution? I checked by using an existing ActiveRecord class which has a corresponding table in the database (eg Customer): Customer.find_all(blah blah) and everything works fine. My problem is that the records return are absolutely no customers. What do you do when you issue a query with find_all and that the data returned doesn''t correspond to any table in the DB and thus any class of your model? Thanks Raph
rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
2005-Aug-29 17:36 UTC
Re: Defining a class for all find_by_sql calls ?
Hello Raphael ! Raphael Bauduin said the following on 2005-08-27 09:08:> What do you do when you issue a query with find_all and that the data > returned doesn''t correspond to any table in the DB and thus any class > of your model?You use the underlying connection, and do select_one or select_all, as in: results = ActiveRecord::Base.connection.select_all( "select * from non_model_table") and you access the values like this: results[:id] NOTE: results contains untyped values - meaning everything is a String. Hope that helps ! François
> and you access the values like this: > results[:id] > > NOTE: results contains untyped values - meaning everything is a String.Slight correction: results is an array of hashes, so you''d use results[0][:id]. Or: results.each do |result| puts result[:id] end -- rick http://techno-weenie.net