Forum: Ruby on Rails newbie question, "show items in selected city and category"

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
ogurec (Guest)
on 2006-12-30 20:20
(Received via mailing list)
rails newbie needs help :)

I have classified ads (cads) related with cities and categories:

class Cad < ActiveRecord::Base
     has_and_belongs_to_many :categories
     has_and_belongs_to_many :cities

class Category < ActiveRecord::Base
    has_and_belongs_to_many :cads

class City < ActiveRecord::Base
    has_and_belongs_to_many :cads

I assume that user can act such way: first, from cities listing, which
is produced by 'list' method in cities_controller, the user chooses
city ( clicks on 'show' link).

cities_controller#show contains the following:
def show
    @city = City.find(params[:id])
    @cads = @city.cads
    @categories_of_cads_in_city = {|cad|

So,  the user sees classified ads and categories of these ads:

<% @cads.each do |cad| %>
<%=link_to cad.title,
		:controller => "cads",
		:action => "show",
		:id =>

<% @categories_of_cads_in_city.each do |category| %>
<%= link_to,
		:controller => "categories",
		:action => "show",
		:id =>

The next thing user may want to do - see classified ads in some
category of chosen city (assume in "Health" category) , so he clicks on
link "Health".
So i must find ads which are associated with chosen city and category
"Health". How can i do achieve this ??
Dan M. (Guest)
on 2006-12-31 00:53
(Received via mailing list)
ogurec wrote:
>     has_and_belongs_to_many :cads
> end
> So i must find ads which are associated with chosen city and category
> "Health". How can i do achieve this ??

This is untested, and there may be an easier way, but this should work:

class Cad
  def self.with_category_and_city(category, city)
    with_scope :find => {:conditions => [" = ? AND = ?",,], :include => [:categories,
:cities]} do

  def self.find_by_category_and_city(category, city, *args)
    with_category_and_city(category, city) { find(*args) }

Set up like this, the Cad.find_by_category_and_city method will take
standard ActiveRecord::Base.find options.

  Cad.find_by_category_and_city(@category, @city, :first)
  Cad.find_by_category_and_city(@category, @city, :all, :order =>

Dan M.
ogurec (Guest)
on 2006-12-31 13:26
(Received via mailing list)
Many thanks Dan,
i've added method 'category' into 'cities_controller' and its works!

I understand how @category gets its value regarding passed id, but how
can i get the value of @city that was previously set in
cities_controller#show ?

@city = City.find(1)
@category = Category.find(params[:id])
@cads = Cad.find_by_category_and_city(@category, @city, :all)
Dan M. (Guest)
on 2006-12-31 19:17
(Received via mailing list)
ogurec wrote:
> @cads = Cad.find_by_category_and_city(@category, @city, :all)
I would pass both the ID for the city and the ID for category to your
'category' action.

Example, in the /cities/show view:
<% for category in @categories %>
  <%= link_to h(, :action => 'category', :id => @city,
:category => category %>
<% end %>

The with the default routes the URL will be like:
And then in the controller for that action:
@city = City.find(params[:id])
@category = Category.find(params[:category])

Dan M.
Ilja I. (Guest)
on 2007-01-01 13:09
(Received via mailing list)
Thanks a lot!
This topic is locked and can not be replied to.