3

I am new for Ruby on Rails, I want to create model and CRUD if my database is already created. I am using mysql database.

Thank you,

5
  • What model you want to create? Commented Jul 1, 2016 at 8:51
  • While diving right in can be a great source of learning, it is often a great source of frustration too. Maybe it is better for you to follow some "traditional" rails tutorials/courses first, so that you gain more understanding of rails in a friendly environment. Only then you should attempt wrapping legacy databases. Rails is notorious for getting in your way if something is not by its conventions (your legacy database naming, for example). You don't need that if you're just starting. Commented Jul 1, 2016 at 8:57
  • 1
    @Kumar , this is my learning period of ruby on rail, normally we are creating new model using this command rails g model user email first_name and its creates model and table but now I have a full database how i will create model Commented Jul 1, 2016 at 9:00
  • What do you mean a full database? Are you trying to use an existing database? Commented Jul 1, 2016 at 9:01
  • 1
    @Kumar yes I want to use an existing database for example I have user table Its have 20 filed using this method we need to add all filed manually, I want there is any method like yii framework and any another mvc frame work its automatically created model Commented Jul 1, 2016 at 9:04

4 Answers 4

3

As you have already tables in db then no need of scaffold or generate model command, as it will generate migration file too.

simple create user.rb file under models folder.

class User < ActiveRecord::Base
end

then run

rails g controller users

this above command will create controller and views for it.

If you want to create new model and CRUD then

rails g scaffold ModelName field_name:data_type field2_name:data_type

above command will generate model, controller with CRUD methods, migration file and views

for more info http://www.tutorialspoint.com/ruby-on-rails/rails-scaffolding.htm

Note: I hope you have configure database connection via config/database.yml file to use existing database

Sign up to request clarification or add additional context in comments.

2 Comments

In my table 30 column , you means we need to specify all fields-name and datatype in this command rails g scaffold ModelName
@RenuThakur : If you have already existing table with 30 column then just add them in strong parameter in controller
2

You can use rails generators to create a scaffold, which will create model, controllers with all CRUD methods and basic views for you, implementing the CRUD actions.

For example, lets say you want to create a student model with name, age and address fields in your database, you'll generate like this

rails generate scaffold Student name:string age:integer address:string

This will generate these files for you

app/controllers/students_controller.rb  # StudentsController
app/models/student.rb  # Student model
app/views/student/ * # all view files for your student model
db/migrate/migrations_file_for_student.rb

You can always create these files manually. And write its methods yourself. Let me know if it helps.

Update

If you have an existing database, make sure you write your you have set up your database.yml file correctly, to connect your app to the right database.

Next create models, but not the migration files(hence you'll have to create user.rb inside app/models manually.)

Test if its working: Open rails console and type

User.all #will list all existing users in the console

to see if everything goes well.

Next you need to create controller and views for your. You can create scaffold_controller instead of controller which also create views for you.

rails g scaffold_controller User email first_name last_name

This will create all the views for you, and users_controller with CRUD methods, but no models or migrations files.

Hope this helps, let me know if there is anything else.

Comments

0

follow below step

  1. rails g model model_name field_name1:field_data_type field_name:field_data_type ex. rails g user name:string email:string
  2. rails g controller controller_name ex. rails g controller users // add specified method in controller and perform operations.

2 Comments

its too long process if we have 20 filed in one table
yes renu, it's long process but @kumar say need to model and perform action from existing database, so need to create model and perform CURD so add particular action in controller.you have any idea for other way please tell me.
0

This is an old question but the answer provided here doesn't work for me as I wanted everything you can typically scaffold. I was trying to find out myself and here's how.

  1. First create the model for the existing table - an empty class like the example below is fine:
class Person < ApplicationRecord
end
  1. Generate the accompanying controller actions and views by running the following:
rails generate scaffold_controller people

The scaffold_controller command is the key, it's not a command you see appear in googled examples!

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.