12

I have tables already created from a different project. Their names are formatted like aaa_bbb_ccc_ddd (all non plural and some parts aren't a convention word). I have successfully created a schema from the database by reading this. But now I have to make the actual models. I've looked at RMRE, but they enforce the ActiveRecord convention on my tables and change their names, which I don't want to do because other apps depend on those tables.

What is the best way to automatically create models and a schema from existing tables?

2 Answers 2

23

just a theory, not sure how this would work in real app:

create models named as ActiveRecord convention requires, for example for table aaa_bbb_ccc_ddd you'll create a model AaaBbb and map this model to your table:

class AaaBbb < ActiveRecord::Base
    self.table_name = "aaa_bbb_ccc_ddd"
end

or a more human example:

class AdminUser < ActiveRecord::Base
    self.table_name = "my_wonderfull_admin_users"
end

Now you'll have AaaBbb as resource in routes meaning you'll have a url like:

 .../aaa_bbb/...

and if you want to use the table name name in url I guess you could rewrite the route:

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

again, just a theory that might help you out. I haven't worked with such cases yet but would've start from this.


edit

to automate model creation from database:

https://github.com/bosko/rmre

but I think this will create models by rails convention with wierd names that you'll have to use as resource in your app.


A good template that I found on SO in case you want to use a model name different from 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
Sign up to request clarification or add additional context in comments.

5 Comments

Is there a way to automate this from a schema.rb? I have ~20 tables with ~10 columns each. Don't want to code names and attributes by hand if possible.
you should care about the tables only and use their columns as they are. I am not sure you can change the columns somehow without affecting the other app that works with this db. About automate I'll think a bout.. will update my answer if I find something usefull.
oh, i wasn't thinking about changing the column names. It's just right now I don't even have a models file since I'm doing stuff backwards. So I'll have to type everything including attr_accessible stuff, so I'd want something to automatically generate ALL the model code for me.
Is backtick (`) used on purpose when setting table name?
@BruceSun I am pretty sure it should be comma.
-3

Just switch from Rails to Django and make your life happier and also make your work normal:

$ python manage.py inspectdb my_table_without_existing_model > some_new_model.py

That's enough. Two seconds of work :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.