Topic: URL and Code Reuse

I am trying to build something similar to craigslist but, being a novice, has some simple questions. I use one table for each city to hold the ads. I have written basic code for one city and it works. But, now I can use only one city "phoenix" as I store all Phoenix ads in this table. But, what is the easy way to do this for other cities like Chicago, Miami etc? My idea for now is to create a new model for each city and may be I can try to reuse the code from Phoenix. If you have better ideas, please suggest me.

Also, now phoenix ad system works at this url hxxp://hostname.com/phoenix. How do I change it to work like craigslist? I mean like hxxp://phoenix.hostname.com, hxxp://chicago.hostname.com and so on.

Appreciate if you can help me out.

Re: URL and Code Reuse

Why are you creating a table for each city? You should place all the cities in one table and have all the ads in another then link them with a one-to-many association:

class City < ActiveRecord::Base
  has_many :ads
end

class Ad < ActiveRecord::Base
  belongs_to :city
end

Railscasts - Free Ruby on Rails Screencasts

Re: URL and Code Reuse

Thanks for your suggestion. My plan is to create a table for each city. Otherwise, I think table gets huge pretty quickly. So, what would be the best way to get this working for urls like hxxp://phoenix.hostname.com?

Re: URL and Code Reuse

Database tables are made to get huge.  A large corporation can easily have a database of several terabytes, consisting of a hundred or more tables, each with tens of thousands of rows with tons of columns.

As for using subdomains - I'd have all the subdomains link to the same web server, then you can probably check the domain in rails (somewhere in request), then lookup the name, and then redirect with the appropriate city id.

Re: URL and Code Reuse

P.S. - an unrelated search just found this:

http://railsforum.com/viewtopic.php?id=4462

Re: URL and Code Reuse

IMHO, you are attempting to prematurely optimize.  I'm with Ryan in that you should not attempt to create tables for each city.  You can handle the urls with routing.  Though, I also wouldn't worry about creating 'fancy' urls right away.

BrewControl.com - Brewery and Brewpub management powered by RoR

Re: URL and Code Reuse

Great guys. Thanks a lot for your help. So, I will go with single table for all cities. Just worried that DB queries might take a long time if table gets huge. You guys have experience and will go with your idea. I have to think about subdomains. Need to do some more research about this before finalizing one.

Re: URL and Code Reuse

I like to follow the principles of Extreme Programming and agile development.  From that, I have learned to do a task as simply as possible.  This may mean starting out with two tables, advertisements & cities.  If your site goes live and becomes popular, you _may_ find you have a performance issue at some point.  In that case, you will probably find that you want to separate the association between cities and ads by adding an indexed map table.  But why do that if it never becomes necessary?  If you force yourself into this mindset for each task that you do, I think you will find like me, that you are getting more things done.

BrewControl.com - Brewery and Brewpub management powered by RoR