开发者

create model from existing postgres sql with relations

开发者 https://www.devze.com 2023-03-18 01:24 出处:网络
Is there a way to generate models from an existing postgresql db with all 开发者_StackOverflow社区the relations?

Is there a way to generate models from an existing postgresql db with all 开发者_StackOverflow社区the relations?

What I have done so far is:

rails generate model existing_table_name

class User < ActiveRecord::Base
  set_table_name :usernames
  has_many :employees
end

But i have so many tables that i dont want to do it manually.

best, p


You could emit that list from a script.

I would honestly just use vim for this task.

Dump your current tables through whatever SQL thing you have, then run some :v//d or :g//d commands, some :%s///s, etc.

Before long you'll have a list that you can then emit something like the above.

Maybe make the vim portion end up at YAML, then use some Ruby to loop over that and output the class definitions.

(BTW I know this is super-old, probably it's been solved and better, but I came across the link so I figured I'd toss out a way of doing it).

0

精彩评论

暂无评论...
验证码 换一张
取 消