# Newbie question, "show items in selected city and category"

**URL:** <https://www.ruby-forum.com/t/newbie-question-show-items-in-selected-city-and-category/80734>\
**Category:** Rails\
**Created:** [December 30, 2006, 7:20pm UTC](https://www.ruby-forum.com/t/newbie-question-show-items-in-selected-city-and-category/80734 "2006-12-30T19:20:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ogurec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/71e660/32.png) [@ogurec](https://www.ruby-forum.com/u/ogurec)\
**Post date:** [December 30, 2006, 7:20pm UTC](https://www.ruby-forum.com/t/newbie-question-show-items-in-selected-city-and-category/80734/1 "2006-12-30T19:20:45Z")

</div>

Hi,  
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  
end

class Category \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :cads  
end

class City \< ActiveRecord::Base  
has\_and\_belongs\_to\_many :cads  
end

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 = @cads.map {|cad|  
cad.categories}.flatten.uniq  
end

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

cities/show.rhtml:  
[…]  
\<% @cads.each do |cad| %\>  
\<%=link\_to cad.title,  
:controller =\> “cads”,  
:action =\> “show”,  
:id =\> cad.id  
%\>

\<% @categories\_of\_cads\_in\_city.each do |category| %\>  
\<%= link\_to category.name,  
:controller =\> “categories”,  
:action =\> “show”,  
:id =\> category.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 ??  
Thanks

---

<div class="post-metadata">

**Author:** ![ogurec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/71e660/32.png) [@ogurec](https://www.ruby-forum.com/u/ogurec)\
**Post date:** [December 30, 2006, 11:53pm UTC](https://www.ruby-forum.com/t/newbie-question-show-items-in-selected-city-and-category/80734/2 "2006-12-30T23:53:46Z")

</div>

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 =\> [“categories.id = ? AND  
cities.id = ?”, category.id, city.id], :include =\> [:categories,  
:cities]} do  
yield  
end  
end

def self.find\_by\_category\_and\_city(category, city, \*args)  
with\_category\_and\_city(category, city) { find(\*args) }  
end  
end

Set up like this, the Cad.find\_by\_category\_and\_city method will take  
standard ActiveRecord::Base.find options.

Example:  
Cad.find\_by\_category\_and\_city(@category, @city, :first)  
Cad.find\_by\_category\_and\_city(@category, @city, :all, :order =\>  
“cads.name”)

Dan M.

---

<div class="post-metadata">

**Author:** ![ogurec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/71e660/32.png) [@ogurec](https://www.ruby-forum.com/u/ogurec)\
**Post date:** [December 31, 2006, 12:26pm UTC](https://www.ruby-forum.com/t/newbie-question-show-items-in-selected-city-and-category/80734/3 "2006-12-31T12:26:16Z")

</div>

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 ?

cities\_controller#category  
@city = City.find(1)  
@category = Category.find(params[:id])  
@cads = Cad.find\_by\_category\_and\_city(@category, @city, :all)

---

<div class="post-metadata">

**Author:** ![ogurec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/71e660/32.png) [@ogurec](https://www.ruby-forum.com/u/ogurec)\
**Post date:** [January 1, 2007, 12:09pm UTC](https://www.ruby-forum.com/t/newbie-question-show-items-in-selected-city-and-category/80734/4 "2007-01-01T12:09:05Z")

</div>

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![ogurec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/71e660/32.png) [@ogurec](https://www.ruby-forum.com/u/ogurec)\
**Post date:** [December 31, 2006, 6:17pm UTC](https://www.ruby-forum.com/t/newbie-question-show-items-in-selected-city-and-category/80734/5 "2006-12-31T18:17:00Z")

</div>

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(category.name), :action =\> ‘category’, :id =\> @city,  
:category =\> category %\>  
\<% end %\>

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

Dan M.
