Tutorial for Queries

Hi! I’m looking for a good tutorial that explains the main points of
performing queries with Rails. I do have AWDWR and have read the
section on ‘find’ but I’m looking for something that goes into more
detail on how to perform queries across tables.

Abstracted from my current application, this is an example:

  • person has_many sites
  • site has_one room
  • room has attributes such as floor#

I’d like to learn how to do things like query how many people older than
30 have an office on a floor higher than Level 7.

I could ask a specific question relating to what I’m doing, but I think
it would be more productive for me to read a tutorial first before I
bother you :slight_smile:

Thanks,
Mohit.

On 13/08/06, Mohit S. [email protected] wrote:

  • person has_many sites
  • site has_one room
  • room has attributes such as floor#

I’d like to learn how to do things like query how many people older than
30 have an office on a floor higher than Level 7.

find :conditions => ‘age > 30 and room.floornumber > 7’

Hasan D. wrote:

find :conditions => ‘age > 30 and room.floornumber > 7’

Cheers,
Hasan D. <[email protected] mailto:[email protected]>
Thanks for the quick response, Hasan! I’m actually also keen to read
more about how these queries are formulated… but, I guess this gives me
a set of ideas to start with :slight_smile:

Cheers,
Mohit.

Hi, I’m trying to build a query that looks like this:

  @book_pages, @books = paginate(:books,
                                             :per_page => 20,
                                             :conditions =>

[‘book.author.name like ?’, '%jim%],
:order_by =>
@sort_order)

I want to basically list out all the books by authors with names like
‘jim’ - prefer to have this paginated so that I can use my basic
paginated list view. However, this throws up an exception:

Mysql::Error: Unknown table ‘books’ in where clause: SELECT count(*) AS
count_all FROM books WHERE (author.name like ‘%ha%’)

What do you think I’m doing wrong?
Thanks
Mohit.