I have a database with tables. I want to create a model in my Rails app from existing table. As i know, such functionality is available, and is done as follows:
script/generate scaffold model_name --skip-migration
Of course, i defined my database in database.yml file. Scaffold generated for me a model with controller and views. My table name is not as it must be for Rails(it is incorrect, not following conventions), i added set_table_name to my controller. But, when i am calling the index method, on my page i have only set of # symbols, but not a data from database. In my index.html.erb i have only generated code by scaffold. How can i print out my database data?
Have you generated a schema file from your existing database? If you run the command
rake db:schema:dump
and then re-generate your scaffold this should fix the problem.
Additionally you may wish to check out Dr Nic's Magic Model generator. This will generate models for all of your existing tables and attempt to guess the relationships. This will probably not work if your table naming is not understandable by rails.
UPDATE
I do not generally use the default scaffold however I have tested this myself and it appears that if you skip the migration and do not pass any column name/type pairs then the scaffold generator will not create anything in the template to render the columns.
You have two choices here either
Pass in the column name pairs as well as skip-migration or
Download Ryan Bates Nifty Scaffold generator which will create the scaffold with the column names even if you specify --skip-migration
Related
New to Rails, so please feel free to edit my question should I get the terminology wrong!
I've created my MVC via scaffold, but have since made changes to the underlying table (renamed some columns, added some columns). Is there a way to regenerate the views that were created with the original scaffold such that they reflect the changes I've made?
If I run rails g scaffold Foo --migration=false --skip I get an error that my model name is already used in my application.
If delete the entire corresponding views folder (the one created by the original scaffold), and I run rails g scaffold Foo --migration=false --force the views generated still don't pick up all the fields I have in my model.
You could reverse the scaffold generation and regenerate a new scaffold with renamed and new columns.
If you already migrated the database you will need to roll it back.
rake db:rollback
Then you can reverse the scaffold generation.
rails destroy scaffold MyFoo
Then generate new scaffold with updated columns
rails generate scaffold MyFoo column_name_1:data_type column_name_2:data_type column_name_3:data_type
Otherwise you can manually rewrite your views and controllers to include the updated and new columns.
I am new to Rails. I want to know the difference between these two methods of generating migrations:
rails g migration MigrationName
rails g model MigrationName
Is preferred for making alterations to the tables that already exists. What is the real difference between these 2?
Well the main difference is that the second one should be rails g model ModelName doesn't just create a migration, but also creates a model file and a spec file for that model. It also will most generate a create table migration, whereas with rails g migration MigrationName, you can just do very specific migrations such as adding an index, or adding/removing columns. Sections 2.1 and 2.2 will help you get a better grasp: http://guides.rubyonrails.org/migrations.html
rails g migration Filename parameters ...
This one generates a file in which you can write migration code. Like creating index or drop, ...
rails g model Tablename field field field:type ....
This on generates a file to generate a table with the give parameters.
See more when running rails g model
I am new to Ruby on Rails . This might be a very foolish question.
I have created a migration using
rails generate migration Kapol name:string position:integer
rake db:migrate
Then using phpmyadmin i copied the database already present
I then opened up rails console
My question is can i use the method Kapol.find(1)??
because when i tried it using singular or plural it says
unitialized constant:Kapol
I know that there has to be a method but where to specify it?
You must generate a model in case to create a table for it, because the migration is usually used to modify existing tables.
It might be confusing that the model generator also creates migration file in your migrations folder. The only difference is that the model generator also generates initial code to create table, on the other hand, the migration generator creates only migration file without initial code.
rails generate model Kapol name:string position:integer
More information: http://guides.rubyonrails.org/getting_started.html#generating-a-model
If you're very new to Ruby on Rails, probably the best thing for you to do is create a scaffold, which gives you your migration file, your model file, your controller file, and various view files, test files, etc. etc., which all work well together. Then you can play with these and build up from there.
rails generate scaffold Kapol name:string position:integer
If you're happy with the migration that was automatically generated, then rake db:migrate and you're all set.
As Andrew says below, you can also just generate any of those files one at a time by replacing 'scaffold' with 'model', etc.
Your Kapol.find(1) is correct.
rails generate migration
rails generate model
rails generate scaffold
rails generate controller etc.
How these differ?
According to rails guides:
Using generators will save you a large amount of time by writing boilerplate code, code that is necessary for the app to work, but not necessary for you to spend time writing. That’s what we have computers for.
rails generate commands family used to provide simple and easy way for developer to create different object types.
rails generate migration - creates DB migration script in db/migrations directory so developer can setup his DB.
rails generate model - creates model class with associated migration, test and fixtures (test data).
rails generate scaffold - creates all nedded classes with basic logic and presentaion. It creates controller (with simple CRUD logic), model, fixtures, functional and unit tests.
rails generate controller - creates controller with associated functional tests, helper and basic views templates.
You can read more here: http://guides.rubyonrails.org/command_line.html#rails-generate
They differ in the sense that they generate different stuff.
migration will generate a database migration file,
model will generate a model(with a migration and a spec by default)
scaffold will generate a scaffold of a resource
and controller will generate a controller.
generate means it will create the files for you with boiler plate code already in place(you will still need to edit them though..but scaffold can get you working with a basic application already)
Read more about it here: http://guides.rubyonrails.org/command_line.html#rails-generate
rails generate is a command line script for quickly generating the code for various Rails' constructs.
In the example you give they differ by what they produce, with the first argument being the type of code generated. For example if I wanted to create a User model I would run:
`rails generate model user`
The model file, test file and migration would be created for me.
You should read the Rails' documentation to find more.
**rails generate model user:
The above command create a Template Object that is a mirror image of the database table.
For example, if you have a database table that is named users that has a name:string, and email:string field,then "rails generate model user" create an Object that mirrors that user table with a few addition.
Here are the similarity they both have name:string,email:string the both have the word user.
The difference are few but significant: user is Capitalized in the model name like "User".
The Table add create_by and updated_by automatically.
migration:db create the database mirror using the model as a model.RECURSION ANYONE?
I have a question to the following procedure:
script/generate scaffold product
title:string description:text
db:migrate
then I generate a migration which adds a column description to the
table products and migrate the db
again.
My question is: why is the field description not added to the project-views? Is that normal rails scaffold behaviour?
I think I saw in a video tutorial that the scaffold updates as well the views, which would be very convenient.
Thanks in advance for any help!
This is normal since scaffold does not "monitor" changes to the table or to any other scaffold-related resource (controller, model, views, tests, etc.).
then I generate a migration which
adds a column description to the table
products
You have description column already when generating the scaffold. Why do you need another migration for this?
This is normal behaviour for scaffolding, however there are alternatives.
If you were to look at Ryan Bates nifty_scaffold generator then this would allow you to re-run the scaffold generation. Assuming that you hadn't changed the generated code.
These generators can regenerate the views/spec/tests based upon the current state of the database model.
Be aware that if you have already customised the views yourself then they could be overwritten.