# Accessing database model from generator?

**URL:** <https://www.ruby-forum.com/t/accessing-database-model-from-generator/167329>\
**Category:** Rails\
**Created:** [May 6, 2009, 7:50pm UTC](https://www.ruby-forum.com/t/accessing-database-model-from-generator/167329 "2009-05-06T19:50:53Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![stewbawka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@stewbawka](https://www.ruby-forum.com/u/stewbawka)\
**Post date:** [May 6, 2009, 7:50pm UTC](https://www.ruby-forum.com/t/accessing-database-model-from-generator/167329/1 "2009-05-06T19:50:53Z")

</div>

i’m writing my own generator 🙂 basically a custom scaffolding type  
thing to make my life easier when creating new resources.

instead of doing something like script/generate custom\_scaffold  
MyResource name:string description:string … i decided to store all  
the metadata about the tables and fields in the db.

my generator creates the model stub files when given a name that  
corresponds to a table that already exists and which has metadata in  
the database. i would like to check in my generator code if metadata  
exists for the table in the DB before going ahead and creating all the  
views, model, controller, etc…

i’m not sure if this is kosher rails coding to have a generator that  
depends on the existence of specifc models/tables to work… but this  
is just for me and i will make sure they are there whenever i use it…

any ideas?

another question… i have a “m.route\_resources file\_name.pluralize”  
line in my generator manifest… is there anyway i can easily check if  
the route already exists? if i run my generator multiple times (which  
is gonna happen a lot since its still in the works) it keeps adding  
duplicate routes in my routes.rb.

Thanks!! happy coding everyone 🙂

Stuart

---

<div class="post-metadata">

**Author:** ![stewbawka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a9adbd/32.png) [@stewbawka](https://www.ruby-forum.com/u/stewbawka)\
**Post date:** [May 6, 2009, 9:27pm UTC](https://www.ruby-forum.com/t/accessing-database-model-from-generator/167329/2 "2009-05-06T21:27:09Z")

</div>

looks like i have a knack for asking dumb questions… you can just go  
ahead and use the models 😛

anybody got a handle on how to check if a resource route already  
exists in routes.rb?

Stuart
