# Querying records. HELP!

**URL:** <https://www.ruby-forum.com/t/querying-records-help/186852>\
**Category:** Ruby\
**Created:** [April 13, 2010, 12:40pm UTC](https://www.ruby-forum.com/t/querying-records-help/186852 "2010-04-13T12:40:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vinjenace](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/d9b06d/32.png) [@vinjenace](https://www.ruby-forum.com/u/vinjenace)\
**Post date:** [April 13, 2010, 12:40pm UTC](https://www.ruby-forum.com/t/querying-records-help/186852/1 "2010-04-13T12:40:57Z")

</div>

I’m new to this language so i don’t know much of it. I’m trying to query  
records to show it in the list view in the web. This is the line of sql  
that I’m trying to produce: SELECT source FROM table WHERE source LIKE  
‘%CMS%’. Can someone help me on this? And can you show displaying the  
records that i’ve gathered step by step?

---

<div class="post-metadata">

**Author:** ![vinjenace](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/d9b06d/32.png) [@vinjenace](https://www.ruby-forum.com/u/vinjenace)\
**Post date:** [April 13, 2010, 12:56pm UTC](https://www.ruby-forum.com/t/querying-records-help/186852/2 "2010-04-13T12:56:23Z")

</div>

Vincent Hernandez wrote:

> I’m new to this language so i don’t know much of it. I’m trying to query  
> records to show it in the list view in the web. This is the line of sql  
> that I’m trying to produce: SELECT source FROM table WHERE source LIKE  
> ‘%CMS%’. Can someone help me on this?

You need a database-access library. Choices include:

- Use the native adapters for mysql, sqlite3 or whatever directly. e.g.  
[http://www.kitebird.com/articles/ruby-mysql.html](http://www.kitebird.com/articles/ruby-mysql.html)

However if you want to change which DB you are using, you’ll need to  
change your code.

- Use ruby-dbi, which lets you use the same API to connect to any of  
these databases, making it easier to port your code between databases.  
[http://www.kitebird.com/articles/ruby-dbi.html](http://www.kitebird.com/articles/ruby-dbi.html)

- Use a higher-level API like ActiveRecord, DataMapper or Sequel, which  
will construct queries for you and/or give you an object representation  
of your result rows.  
[http://ar.rubyonrails.org/](http://ar.rubyonrails.org/)

> And can you show displaying the  
> records that i’ve gathered step by step?

In the old days you might have written a CGI or FastCGI program to do  
this. In the new world you’ll probably want a web framework like Sinatra  
(small and simple) or Rails (large and featureful)

A simple Sinatra program looks like this:

require ‘sinatra’  
get ‘/’ do  
“hello world” # \<= replace with code to query your DB  
# and generate a HTML page with table  
end

You probably also want to look at a templating library to help you  
insert dynamic content into your HTML pages, like ERB (similar to PHP,  
insert Ruby directly into your HTML) or HAML. Sinatra and Rails both  
have helper methods to let you use these templating systems, and others  
are available as plugins.

If you don’t really know where to start, Rails is probably the best, due  
to the huge amount of documentation available. Some good starting points  
are

> **[Ruby on Rails Guides](https://guides.rubyonrails.org/)**
>
> Ruby on Rails Guides

> **[Ruby on Rails Screencasts - RailsCasts](http://railscasts.com/)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

The tutorial which got me hooked was

> **[O'Reilly Media - Technology and Business Training](https://www.oreilly.com)**
>
> Gain technology and business knowledge and hone your skills with learning resources created and curated by O'Reilly's experts: live online training, video, books, our platform has content from 200+ of the world's best publishers.

although it’s pretty old now (for Rails 1.2.x, whereas Rails 2.3.x is  
now current and 3.x is about to be released)

Note that Rails is a huge piece of code in its own right, and questions  
about Rails are best asked on a Rails mailing list. The Ruby mailing  
list is more for general questions about the Ruby programming language.

HTH,

Brian.
