activerecord - Rails: Creating models from existing tables? -


i have tables created different project. names formatted aaa_bbb_ccc_ddd (all non plural , parts aren't convention word). have created schema database reading this. have make actual models. i've looked @ rmre, enforce activerecord convention on tables , change names, don't want because other apps depend on tables.

what best way automatically create models , schema existing tables?

just theory, not sure how work in real app:

create models named activerecord convention requires, example table aaa_bbb_ccc_ddd you'll create model aaabbb , map model table:

class aaabbb < activerecord::base     self.table_name = "aaa_bbb_ccc_ddd" end 

or more human example:

class adminuser < activerecord::base     self.table_name = "my_wonderfull_admin_users" end 

now you'll have aaabbb resource in routes meaning you'll have url like:

 .../aaa_bbb/... 

and if want use table name name in url guess rewrite route:

get 'aaa_bbb_ccc_ddd/:id', "aaa_bbb#show", as: "aaa_bbb"

again, theory might out. haven't worked such cases yet would've start this.


edit

to automate model creation database:

https://github.com/bosko/rmre

but think create models rails convention wierd names you'll have use resource in app.


a template found on in case want use model name different table name:

class youridealmodelname < activerecord::base   self.table_name = `actual_table_name`   self.primary_key = `id`    belongs_to :other_ideal_model,      :foreign_key => 'foreign_key_on_other_table'    has_many :some_other_ideal_models,      :foreign_key => 'foreign_key_on_this_table',      :primary_key => 'primary_key_on_other_table' end 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -