I have a model called MyArticle. When I try to use the command
rails generate migration AddtestToMyArticle test:string
the migration file contains only empty up/down methods. Having done this previously on a single word model name, it worked just fine and the migration up/down methods had the appropriate code.
I tried "AddtestToMy_Article" but that didn't work either. What do I need to do to work with my compound model name and the generate migration command?
You need to use
rails generate migration AddColumnToMyArticle test:string
When using 'AddColumn' you will have the appropriate code in your migration.
I just looked back over this, and while my answer is correct, it's better to have a more descriptive migration name. The user below who noticed the capitalization is right in that if you don't have each new word capitalized, Rails won't pick up on what exactly you're trying to do. So, in your question you have AddtestTo... but it should be AddTestTo....
It seems to work if you use underscores rather than CamelCase
rails g migration add_test_to_my_article test:string
Hope this helps
rails generate migration AddNewFieldToMyArticle new_field:string
Related
What is the best possible way to change name of table using migration and change name of all the files like controller, model and associations?
Will there be any issue when someone will try to run rails:db:migrate after cloning my repo?
What is the best possible way to change name of table using migration
To change the name of a table, you can run:
$ rails g migration change_[old_table_name]_to_[new_table_name]
Within the change method in the migration file generated, add this:
def change
rename_table :[old_table_name], :[new_table_name]
end
Change [old_table_name] and [new_table_name] in both cases.
(This part of the question has been answered here.)
will there be any issue when someone will try to run rails db:migrate after cloning my repo?
Nope. Keep the old migration files in place and generate a new one. That's the benefit of database migrations.
What is the best possible way to change name of all the files like controller, model and associations?
It's generally not too big of a deal to change a model name. Many text editors have the ability to search and replace within a directory.
And I would manually rename the filenames.
Here's a set of more detailed steps to make sure you've hit everything that needs to be changed.
I am trying to access all records of a table that has an underscore in its name.
For example if I have table in my schema that is called trips I can do Trip.all in rails console. But what do I do if my table name contains an underscore (e.g. users_foods)
I tried the following options:
Users_food.all
User_food.all
User_foods.all
etc.
All of the above did not work, any suggestions?
Figured it out
One can access the data with UserFood
For a table named users_foods, ActiveModel should provide you with a corresponding Rails model UsersFood, to fit Ruby/Rails object naming convention. Try that.
Class names Users_food or User_food, etc. do not conform to Ruby convention.
I had the same problem when creating bonus_histories table. And didn't work any of the answers until I found out why.
I have made only rails g migration BonusHistory
and it was the problem. My rails console didn't find BonusHistory at all because I had no model.
So I had to first rollback the migration rake db:rollback STEP=1, then deleted the migration file and finally made
rails g model BonusHistory
and after migrating that table, when I enter rails console, I can successfully ask for BonusHistory.count
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.
I have a bunch of rails models that I'm re-writing into a single model to simplify my code and reduce unnecessary tables.
I'm wondering what the best way to delete a model class and its table is. I want past migrations to still succeed, but I don't want to leave the empty models lying around.
Do I have to manually delete the old migrations that reference these models, then manually delete the class files?
Does anyone have any tips for the best way to do this?
All in one solution.
Run the following commands:
rails destroy model ModelName
rails g migration DropTableModelName
The former will generate a new migration file which should looks like this:
class DropTableModelName < ActiveRecord::Migration
def change
drop_table :model_name
end
end
Now run db:migrate and you're done.
If you'd like to completely get rid of of a model and its table do this:
rails destroy model Name
The question is a bit stale now, but I just did:
rails destroy scaffold <ModelName> -p
The -p flag shows "pretend" output, which is good for seeing what will happen. Remove the '-p' flag and the results will match the output. This cleaned the entire collection of M-V-C files + testing + js files + the original migration, no problem.
I guess if you are one who likes to hand edit your migrations and include multiple steps in each, losing the original migration could break db:setup, so buyer beware. Keeping one action == one migration file should avoid this potential snafu.
What about doing ruby script/destroy model? That should take care of the model and the migration.
Depending on how far into development or production you are, you may want to migrate the models out safely using a migration to remove/backup data or what not. Then as bobbywilson0 suggested, using
script/destroy model
or if you rspec anything
script/destroy rspec_model
This will remove any spec tests as well.
Or you can always just drag them to the trash folder.
You can take a look at this one at rails guide.
But I suggest, if it is possible, you should delete the models and all references to the models. This will probably save time later as you don't need to maintain the dead code in the codebase.
If you'd rather have a manual based answer:
First run the following command to identify which migrations you want removed:
rake db:migrate:status
Feel free to grep -i on it too if you're confident of your naming scheme.
Make note of all the "add x to model name" and similar alterations to your Model. These can be removed using:
rails d migration AddXToModelName
Do this for every migration besides the initial create migration. The following command will take care of the initial create migration and the files associated with the model:
rails d model ModelName
I'm new to RoR, and I've just used scaffold to generate a table and create the pages for CRUD operations. Now I want to add a new field to this. One place I found tells me how to do that in the database, but is there a way to do it where it will add the field to all the pages too, or is that just a manual operation and I need to make sure I know all my fields up front?
To add a new column to the database
$ script/generate migration add_fieldname_to_tablename fieldname:string
$ rake db:migrate
To get your views up to date you can run the scaffold again, with your updated list of fields. It will balk on replacing your migrations but you can force it to replace your views.
$ script/generate scaffold tablename fieldname:string old_field_1:string ...
At the prompt answer a and it will overwrite the views, but not the old migration. It also won't modify your existing data.
First you'll write a migration to add the field, run the migration, then you need to rerun the scaffold to regenerate the views, etc. Beware, this will wipe out edited files from before. Of course, instead of scaffolding again you could manually add references to new field where appropriate.
You will have to update your database, no matter what (Remember to 'rake db:migrate' after you create the migration!)
Regarding the interface, you are a bit more lucky: the formtastic plugin makes your views look like this:
The 'f.inputs' is calculating the form fields on-the-fly, based on your model's attributes. This will not cover complex forms that need special treatment, but the usual ones, you will get them automatically.
For an easy-to-understand tutorial, see the latest railcast (Railscast #184, you will have to google for it, I can't post 2 links because I'm not cool enough for stackoverflow yet, sorry).
Railcast #185 is supposed to continue covering formtastic, and it's due to be published next monday.
Needs to be done manually, or the scaffold needs to be regenerated.