I usually add class methods for things like this to the relevant model.
Here is the model of Articles on my weblog typo
( accessible at svn://leetsoft.com/typo/trunk )
class Article < ActiveRecord::Base
has_many :comments
def self.categories
articles = find_by_sql("SELECT distinct category FROM articles")
articles.to_a.collect{ |a| a.category.to_a}.compact.flatten
end
def self.search(query)
if query
tokens = query.split.collect {|c| "%#{c}%"}
find_by_sql(["SELECT * from articles WHERE #{ (["body like
?"] *
tokens.size).join(" AND ") }", *tokens])
end
end
protected
before_save :transform_body
def transform_body
self.body_html = HtmlEngine.transform(body)
end
end
On Wed, 19 Jan 2005 02:04:51 -0300, Demetrius Nunes
<demetrius-fDpYTK8McCzCdMRJFJuMdgh0onu2mTI+@public.gmane.org>
wrote:> David van den Berg wrote:
>
> >>
> >> Is there a way to get Active Record to use raw sql. What I need
is
> >> to pull records from a mySQL database, based on a GROUP BY
> >> statement. The result will be read-only of course. I can do this
> >> manually, but would like to use a similar mechanism to the rest of
my
> >> app
> >
> You can use the ActiveRecord::Base.connection#select_all to do that.
>
> Rgds
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
--
Tobi
http://www.hieraki.org - Open source book authoring
http://blog.leetsoft.com - Technical weblog