# Dynamic Table Selection in Rails

**URL:** <https://www.ruby-forum.com/t/dynamic-table-selection-in-rails/107472>\
**Category:** Rails\
**Created:** [June 22, 2007, 3:12am UTC](https://www.ruby-forum.com/t/dynamic-table-selection-in-rails/107472 "2007-06-22T03:12:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mfaine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b3f665/32.png) [@mfaine](https://www.ruby-forum.com/u/mfaine)\
**Post date:** [June 22, 2007, 3:12am UTC](https://www.ruby-forum.com/t/dynamic-table-selection-in-rails/107472/1 "2007-06-22T03:12:12Z")

</div>

I’m looking at various technologies in an effort to build a very  
flexible but also very simple application.

A requirement for the application is to take the database table name  
(or in this case a view) as a parameter. The database will be Oracle  
10.2. The application will simply list (and paginate) records in a  
view that is composed from two tables. The tables contain information  
about files on the file system of the application server. (filename,  
path, title, subject, etc)

The requirement is important because it ensures that one web  
application can handle many customers. Customers are all inter-  
departmental within an organization and would not care if their data  
is inadvertently exposed by a mistyped URL, all of the data will be  
publicly available and not restricted.

As an example I’d like it to work something like this, the URL to a  
retrieve a given listing would be:

[http://servername.domain/List/microgravity](http://servername.domain/List/microgravity)  
or  
[http://servername.domain/List/ssme](http://servername.domain/List/ssme)

The last bit of the path would be the database to use. From my very  
limited knowledge of Rails this would not be practical in Rails. What  
do you think?

Thanks,  
-Mark

---

<div class="post-metadata">

**Author:** ![mfaine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/b3f665/32.png) [@mfaine](https://www.ruby-forum.com/u/mfaine)\
**Post date:** [June 22, 2007, 3:09pm UTC](https://www.ruby-forum.com/t/dynamic-table-selection-in-rails/107472/2 "2007-06-22T15:09:34Z")

</div>

On Fri, Jun 22, 2007 at 01:11:10AM -0000, mfaine wrote:

> I’m looking at various technologies in an effort to build a very  
> flexible but also very simple application.
> 
> A requirement for the application is to take the database table name  
> (or in this case a view) as a parameter. The database will be Oracle  
> 10.2. The application will simply list (and paginate) records in a  
> view that is composed from two tables. The tables contain information  
> about files on the file system of the application server. (filename,  
> path, title, subject, etc)  
> […]  
> As an example I’d like it to work something like this, the URL to a  
> retrieve a given listing would be:
> 
> [http://servername.domain/List/microgravity](http://servername.domain/List/microgravity)  
> or  
> [http://servername.domain/List/ssme](http://servername.domain/List/ssme)
> 
> The last bit of the path would be the database to use. From my very  
> limited knowledge of Rails this would not be practical in Rails. What  
> do you think?

It’s quite doable, and not too difficult. You can choose to do it with  
or  
without ActiveRecord. With AR, you’ll need to dynamically create  
classes.  
That isn’t all that difficult, but it can become a memory leak if you  
aren’t careful. Without AR, you’ll just connect to the DB directly and  
perform queries against it, displaying the result sets. It doesn’t sound  
like you’ll be treating the rows as models, so this may be appropriate.

For the routing, you just need ‘/List/:tablename’ to map to a particular  
controller and action and use params[:tablename] in the action  
implementation.

> Thanks,  
> -Mark  
> –Greg
