bosko / rmre

Rails 3 models reverse engineering
MIT License
126 stars 36 forks source link

Guidance using rmre #19

Closed apple-corps closed 10 years ago

apple-corps commented 10 years ago

This isn't necessarily a feature request, more of a request on how to use rmre. I'm a rails / ruby n00b, and I was able to import and test a postgresql database following the documentation provided. Just to be explicit I will include my sql scheme and the rmre generated files below. Please excuse any incorrect assumptions that I've made.

I've been following the text 'agile web dev with rails 4'. Following the text when I start a new project using 'rails new projectname', it seems to create a database user 'projectname' and some databases 'projectname_development' , 'projectname_test',and 'projectname_production'

Then generating models from 'rails generate scaffold modelname \ fieldname1:type1 fieldname2:type2' populates the projectname databases with tables based on the given fields. It also provides view, test, controller, etc, 'helper code' for the given model.

What I'd like to do is have the benefits of having 'helper code' from using 'rails generate ...' and having my databases be populated in the paths created by 'rails new projectname'

Would the best way to go about this be to use the models that rmre generates as a basis on how to determine the fields to give to the 'rails generate ....' and then to copy my database into the databases created by 'rails new projectname' e.g. 'projectname_development' , 'projectname_test', and 'projectname_production' ?

If not can somebody suggest how to go about what I'd like to accomplish?

::rmre_db.rb::

require "yaml"
require "active_record"

dir = File.join('./', '*.rb')
Dir.glob(dir) { |file| require file }

def connect
  settings_file = './rmre_db.yml'
  exit unless File.exists?(settings_file)
  settings = YAML.load_file(settings_file)
  ActiveRecord::Base.establish_connection(settings[:db])
  ActiveRecord::Base.connection
end
::rmre_db.yml ::

:db:
  :username: discord
  :password: ''
  :port: 
  :timeout: 5000
  :adapter: postgresql
  :database: interrogator
:out_path: ./
::dict.rb ::
class Dict < ActiveRecord::Base
    self.table_name = 'dict'
    self.primary_key = :key

    has_many :definitions, :class_name => 'Definition', :foreign_key => :key    
    has_many :comments, :class_name => 'Comment', :foreign_key => :key    
    has_many :json_verbs, :class_name => 'JsonVerb', :foreign_key => :key    
    has_many :phrases, :class_name => 'Phrase', :foreign_key => :key    
end
::definition.rb ::

class Definition < ActiveRecord::Base

    belongs_to :dict, :class_name => 'Dict', :foreign_key => :key    
end
::comment.rb::
class Comment < ActiveRecord::Base

    belongs_to :dict, :class_name => 'Dict', :foreign_key => :key    
end
::json_verb.rb ::

class JsonVerb < ActiveRecord::Base

    belongs_to :dict, :class_name => 'Dict', :foreign_key => :key    
end
::phrase.rb ::

class Phrase < ActiveRecord::Base
    self.table_name = 'phrase'

    belongs_to :dict, :class_name => 'Dict', :foreign_key => :key    
end
::postgresqlschema.sql ::

CREATE TABLE dict(
  key varchar(80) primary key 
);

CREATE TABLE definitions(
  key varchar(80) references dict(key) NOT NULL,
  definition json NOT NULL,
  adj boolean NOT NULL,
  adv boolean NOT NULL,
  amer boolean NOT NULL,
  animal boolean NOT NULL,
  animate boolean NOT NULL,
  art boolean NOT NULL,
  collective boolean NOT NULL,
  coll boolean NOT NULL,
  com boolean NOT NULL,
  conj boolean NOT NULL,
  dow boolean NOT NULL,
  exclam boolean NOT NULL,
  f boolean NOT NULL,
  formonly boolean NOT NULL,
  human boolean NOT NULL,
  impersonal boolean NOT NULL,
  inanimate boolean NOT NULL,
  interj boolean NOT NULL,
  intr boolean NOT NULL,
  irreg boolean NOT NULL,
  it boolean NOT NULL,
  m boolean NOT NULL,
  mf boolean NOT NULL,
  month boolean NOT NULL,
  n boolean NOT NULL,
  phrase boolean NOT NULL,
  possessive boolean NOT NULL,
  prep boolean NOT NULL,
  preposition boolean NOT NULL,
  prepphrase boolean NOT NULL,
  pro boolean NOT NULL,
  pronoun boolean NOT NULL,
  reg boolean NOT NULL,
  reflex boolean NOT NULL,
  reflexive boolean NOT NULL,
  r61 boolean NOT NULL,
  season boolean NOT NULL,
  slang boolean NOT NULL,
  sp boolean NOT NULL,
  timespan boolean NOT NULL,
  too  boolean NOT NULL,
  tr  boolean NOT NULL,
  v  boolean NOT NULL,
  vai  boolean NOT NULL,
  vde  boolean NOT NULL,
  ven  boolean NOT NULL,
  vqu  boolean NOT NULL,
  vr  boolean NOT NULL,
  vt  boolean NOT NULL,
  vulgar  boolean NOT NULL,
  whenn boolean NOT NULL
);

CREATE TABLE comments(
  key varchar(80) references dict(key) NOT NULL,
  comment varchar(1600) NOT NULL
);

CREATE TABLE json_verbs(
  key varchar(80) references dict(key),
  pde  json NOT NULL,  
  imp  json NOT NULL,
  pre  json NOT NULL,
  fut  json NOT NULL,
  pos  json NOT NULL,
  pds  json NOT NULL,
  ids1 json NOT NULL,
  ids2 json NOT NULL
);

CREATE TABLE phrase(
  key varchar(80) references dict(key)
);
bosko commented 10 years ago

I'm not sure if I understood completely what you want to accomplish but I'll try to answer your question. From your comment it seems you want to use Rails on the existing, legacy, database. If that's true that's what RMRE is meant to be used for. When you create new application Rails automatically creates database related configuration files. Rails does not create neither database user not database. Since Rails follows 'convention over configuration' principle it will set up configuration files with database names same as application name but with different suffixes that correspond to various environments (i.e. '_development' or '_test').

Database configuration file is config/database.yml. You can freely edit it and set any name you want for your database user and databases (schemas). Good practice is to use different database (schema) names for different environments. Once you finish configuring application you can use RMRE to generate all model files. You do not have to use scaffold. RMRE will read all the data from database, create model files and place them wherever you tell it to. In your case you can use ./app/models as output folder. Since you already have database you don't have to use Rails generator and you do not have to pass any fields to it.

Finally when you create all models you can use Rails generator to create controllers and corresponding views but it will not touch models. For example you can execute:

rails generate controller Definitions index show edit view

I hope this answers your question. If I missed something let me know and I will try to answer again.

apple-corps commented 10 years ago

Thank you very much. I had started with a database that I created parsing a text file in Python. I then wanted to use this database with rails. I only wanted to make sure that I could use the power of rails helper methods and management functionality with the database. It sounds like I can accomplish this and I understand how rails works a little better. I can now continue with my project. Thanks again for the help !

apple-corps commented 10 years ago

Hi, I have fooled around with this quite a bit today. Really I want the power of the scaffolding, but to be able to use the data that I have in an existing database.

After trying the generate controller that you showed above, I realized that it just created a bunch of empty html.erb files. This is quite different from what happens when I use scaffolding. When using scaffolding, The html.erb files are populated with code which displays entries from the database, a form is created to add entries to the database and much more. I might mention I'm using rails 4.

What I'm trying to achieve is the ability to scaffold or have the benefits of the helper code, and be able to import my database. Maybe I should try what I suggested before, creating the scaffold based on the attributes listed in the models from rmre. Then configuring the database or dumping it into the one created by rails in order to make it work.

bosko commented 10 years ago

You don't need any attributes during scaffolding. You can scaffold without attributes which will result in dummy migrations, which you anyway do not need since you have database, but will create everything else. Later just generate all models with RMRE and copy them to app/models folder.