rails 3:how to generate models for existing database tables
A Rails model doesn't show your fields, but you can still use them. Try the following. Assuming you have a Model named ModelName and a field called "name", fire up the Rails console and type:
ModelName.find_by_name('foo')
Given a name that exists in the DB, you should see results.Rails doesn't infer relationships though, but if your database follows Rails conventions they are easily added.
Update
I've noticed this particular lack of explicitness ("magic") is a source of confusion for newbies to Rails. You can always look in schema.rb
to see the models and all the fields in one place. Also, if you would prefer to see the schema for each model in the model file, you can use the annotate_models gem, which will put the db schema in a comment at the top of the model file.
Generate models from existing tables using Rails 3
It's not that difficult, just takes a bit more configuration. Here's a basic template for a model:
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
Rails Generate Model from Existing Table?
Rails is very opinionated, so if you have a table called "person" and you want the corresponding model to be called Person, you need to tell Rails explicitly not to be so clever (otherwise, it will assume that it needs to look for the plural of the model name for the table name).
class Person < ActiveRecord::Base
set_table_name 'person'
end
If your table's primary key isn't called "id", then you'll need to specify that, too...set_primary_key 'person_id'
You may also need to specify a different autoincrement sequence name, depending on your database.There's not a way that I know of to automatically generate a model from an existing legacy table, but this should get you most of the way there.
Ruby On Rails: Create Models View And Controller from existing database
You have to create simple model for every table with relations, and then you can
[rails3] > rails generate scaffold_controller Club name:string exclusive:boolean
create app/controllers/clubs_controller.rb
invoke erb
create app/views/clubs
create app/views/clubs/index.html.erb
create app/views/clubs/edit.html.erb
create app/views/clubs/show.html.erb
create app/views/clubs/new.html.erb
create app/views/clubs/_form.html.erb
create app/views/layouts/clubs.html.erb
invoke test_unit
create test/functional/clubs_controller_test.rb
Alternatively you can try active_admin gemActiveAdmin - https://github.com/gregbell/active_admin
rails generate active_admin:resource [MyModelName]
RailsAdmin is also good enough https://github.com/sferik/rails_admin
You should specify at least 2 rules for your model if it doesn't use rails conventions.
Example
class Article < ActiveRecord::Base
self.table_name "tbl_articles"
self.primary_key "art_id"
end
ruby on rails: How to create table for a new model
The order of your fieldname:type combo is incorrect. Try
rails generate model mynewmodel name:string description:string
Related Topics
What Ruby Technique Does Rails Use to Make My Controller Methods Render Views
Undefined Method 'Merge' for '####':String <%= Form_For %> Helper
Is The Assignment Operator Really "Just" an Operator
How to Remove Backslashes from a JSON String
Nomethoderror: Undefined Method 'On' for Main:Object
What's an Example of Ruby Code That's "Too Clever"
Ruby: Is It Acceptable to Put More Than One Class in a File
Encoding Problems in Rails on Ruby 1.9.1
Ruby Gem Development - How to Use Activerecord
How to Send an Image on The Web in an Xmpp (Jabber) Message
How to Directly Install a Gem from a Git Repository
Rails Redirecting Invalid Route to Root
Ruby: How to Convert an Array of Data to Hash and to JSON Format
How to Get All Message History from Hipchat for a Room via The API
Docker-Compose Restart Connection Pool Full
How to Properly Manage The Rails Tmp Directory