Joining on multiple tables

I’m having trouble trying to join on multiple tables. In this example,
I want to do fetch of companies in a specified category and location.
The pagination code is using the will_paginate plugin. Any pointers?
Do I need to resort to a find_by_sql?

  @category = Category.find(params[:category_id])
 @location = Location.find(params[:location_id])
  #Need to join on location id too, how?
  @companies = @category.companies.paginate :all, :per_page =>

10, :page => params[:page]

Thanks,
Tommy

Hi Tommy,

There is a will_paginate group:
http://groups.google.com/group/will_paginate

Ask your question there, please.
Thanks,

  • Mislav

Can you use a dynamic, attribute-based finder method with your has_many
declaration? Or use an association extension if Rails doesn’t create it
automatically. Like this:

@category.companies.find_by_location(@location)

This method won’t be terribly fast though since you are performing 3-4
queries. I would consider a specialized query method like
“Company.find_by_category_and_location(cat_id, loc_id)”.

mike

wheels wrote:

  @category = Category.find(params[:category_id])
 @location = Location.find(params[:location_id])
  #Need to join on location id too, how?
  @companies = @category.companies.paginate :all, :per_page =>

10, :page => params[:page]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs