On Fri, May 22, 2015 at 12:44 PM, kenatsun <kenat...@gmail.com> wrote:
> I'm evaluating Rails (vs Django) for a new project.  A requirement of this
> project is that the Rails app(s) be able to work with a database
> (PostgreSQL, in case that matters) whose schemas (table definitions) have
> been created and, over time, will be modified independently of the Rails
> app(s).
>
> Though I'm a newbie to Rails, it already seems to be that Rails' standard
> mode of operation is:  (1) Models are created and modified within Rails, and
> then (2) Rails Migration creates and modifies the database table
> definitions.
>
> What I need is the opposite sequence:  (1) The database table definitions
> are created and modified through tools external to Rails, and then (2) the
> Rails models are created and modified to fit the database table definitions.
>
> So my questions are:
>
> 1.  First and most important, is there a Gem (or some other tool in the
> Ruby-Rails world) that can generate a model from a database table
> definition?

If you have a table named "things", all you need is  a file named e.g.
thing.rb that contains

class Thing < ActiveRecord::Base
end

That will give you access to all the fields of that table.

> 2.  If not, presumably one could use Rails on a pre-existing database by
> manually editing the Rails models.  Right?

See above.

> 3.  If that's right, what are the requirements for the table designs to
> enable them to work as viable Rails models?  It seems that one requirement
> is that each table have a sequence-assigned primary key named "id".  Right?

Not exactly. You do need a primary key, and it's *easiest* with an
integer, and *easiest* if the name is "id", but not necessary; I've got
an app using string ids based on a legacy db schema (from what
was previously a Java app).

> Any others?

Depends on what you're trying to do. I'd suggest just trying it :-)

The "schemas independently modified over time" part presages
some future pain, I would think; make sure you have *really good*
test coverage.

HTH, and good luck.
-- 
Hassan Schroeder ------------------------ hassan.schroe...@gmail.com
http://about.me/hassanschroeder
twitter: @hassan
Consulting Availability : Silicon Valley or remote

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/CACmC4yBR8yXMgR6DmNZVt99BCZQ3wSOdnOmzZWDqubZJyvL0Pw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to