how to load and make app scaffold from database in rails - mysql

I am using mysql database with following tables and columns:
table masterProduct: (id,name,description, image).
everything is already set&ready in database.yml(connected with database) file, and in Gemfile (gem 'mysql').
When i do :rails new Product , i would like to load all the info i have in database but as scaffold, so i can edit/delete items which are already there or add new ones.
I've been looking on some tutorial like, http://www.tutorialspoint.com/ruby-on-rails/rails-scaffolding.htm but it didnt help me a lot, since he created a database manually, and i used PHPMADMIN.
Any sugestions?
Thanks,
Michael.

Manage your database manually is very bad idea. Rails has awesome features to do this like migrations, seed, etc. When you need update your db schema, you create a new migration to do that. This is the best and easy way. But...
Dynamic scaffold was removed in Rails 2. However you have some options:
1) You can try the gem activescaffold. Other gems like rails_admin or activeadmin are awesome to generate admin views.
2) When you execute
rails generate scaffold Product id:integer name:string description:text image:string
the generator will generate the scaffold files with a migration to create your table. So you can execute rake db:migrate to update your database schema. You can try execute this generate without execute the rake command once your table already exists. This will not create the file db/schema.rb and you will need to manager database manually (bad idea).

Related

rails db:migrate doesn't work after altering the database manually

I am still new to rails and I have some questions regarding rails migration.
I am using rails 5, windows 8. I generated a model and wrote some code which creates a table with columns. Then I deleted that table from my development db (MySQL) and tried to execute db:migrate again to see if it would create the table and columns written in the migration file. After executing db:migrate, it didn't show any messages in the terminal and it did not create the table and the columns.
Based from my observation, deleting the version of my migration file from schema_migrations and running db:migrate once again, it worked and created the table and columns.
I would like to ask the pros of rails development on why is this happening and what should I do when I start to develop a large database model.
I would also like some suggestions about great resource materials for learning ruby on rails.
Thanks!
Its because rake db:migrate is running only migrations for the current env that have not run yet. So if you run db:migrate and then manually delete table form DB, your app does not know that.
MIgration Guides

Use MySQL workbench to create tables and data for rails app

Can I use MySQL Workbench to create the tables and add data then import / connect that into my Rails app? My Rails app is already connected to MySQL. I am just wondering if I create tables in MySQL Workbench through the app_development schema if that will sync over to my Rails project or not and if there's a way to check to see if it worked?
You can use
rake db:schema:dump
to generate schema.rb from an existing database.
Having schema.rb, you can generate an empty initial migration and put create_table and other statements from generated schema there. Doing this will allow you to use all the relevant rake tasks just as if you developed your database "the Rails way" — via a series of migrations.

how to create migration for already exist table in rails

I have deleted the migration files in rails project. Now i want run the rails project in another system for that i need the migration files to create the table. please tell me how to create the migration files for already created tables in rails.
You still have db/schema.rb file containing all tables and their details..you can use it by rake db:schema:load,this will load the schema into the database, which is faster than running all the migrations.
NEVER DELETE YOUR MIGRATION AND MAINTAIN IT IN SUBVERSION/GIT

Connecting already existing MySQL table to already existing Scaffold

So, I know that it is certainly possible to connect an already-existing MySQL database to a completely new Rails application through rake db:schema:dump and by configuring the database.yml file, as shown in a couple of Stack Overflow questions and through this blog: Ruby on Rails Tutorial: Creating a Rails Instance from an Existing MySQL DB.
However, is it possible to link an already existing scaffold in Rails 4.1 - one that is generated through say,
rails g scaffold person \
firstname:string lastname:string gender:string age:integer
(and this scaffold is generated with the default SQLite setting), and say you have a MySQL database on AWS named people with columns firstname:string lastname:string age:integer.
Is there some way to link up the rails app to this existing database in such a case?

schema.rb got mysteriously changed into another file

Using rails 3.2.8 ruby 1.9.3 p362.
There is something seriously amiss with my rails app. After so many db issues with an app, I STARTED FROM SCRATCH - read: rails new < new app name>
And started adding scaffolds and models. All in the past hour or so.
Then I tried to migrate a model called Product and I got the "Mysql2::Error: Table 'products' already exists" message when I ran rake db:migrate
Lo-and-behold my schema.rb file transformed itself into the mess of a file I had on my old app (on a different folder).
How can this happen? And how can I fix it?
Also, how often should I be pushing to git? I pushed once before building 2 new models and I already have a mess in my hands.
Help, please!
Did you clean up the old database for migrations from your older app / create a new database for the new app? Looks like it is the older database which is causing the issues.
You can go to your mysql prompt, and do a drop database database_name; and recreate an empty database using create database database_name.