I am pretty new to Ruby on Rails, and I was wondering if there was a way to edit the database schema for a model.
For example, I have the Subscriber model in my application -- the way I created it was by using rails generate scaffold Subscriber email:string
But now, I want a name in the subscriber model as well. Is there any easy way to do this? I have put a lot of code in my current controllers and views, so I don't necessarily want to destroy the scaffold, but I would like to edit the model.
Thanks in advance,
hwrd
P.S. I am using Ruby on Rails 3
An ActiveRecord Model inspects the table it represents. You don't actually need to change your model just to add a new field (unless you want to add validations, etc).
What you want to do is make a new migration and then migrate your database up:
rails g migration AddNameToSubscribers name:string
rake db:migrate
Then you can start referencing the name field in your controllers and views.
(This generator command might seem a little magical, but the rails generator recognizes this format and will generate the appropriate add_column and remove_column code. See the Rails migration guide for further reading.)
If you mean changing the database schema of your model, you'll want to use migrations.
You'll do things like
add_column :city, :string
remove_column :boo
http://guides.rubyonrails.org/migrations.html
If you do only mean finding models and updating the data inside each instance, go with #apneadiving's answer.
Related
I am currently working on a rails app where we are using mongoid/mongoDB on the back-end. I understand that I don't need ActiveRecord like migration to migrate the schema, but I do need to migrate data as I change mongoid model definitions. Is anyone else out there running into the same scenario, if so how are you handling it?
Even though you're not making schema changes, you may need to move data between fields, or remove fields that are no longer used in the codebase. It's nice to have migrations that you can run when you deploy new code. I recommend using a gem called mongoid_rails_migrations. This provides you with migration generators like you're used to and provides some organization to migrating data.
class MyMigration < Mongoid::Migration
def self.up
MyModel.all.each do |model|
# label was renamed to name
model.set :name, model[:label] # copy the data from the old field to the new one
model.remove_attribute :label # remove the old field from the document
model.save!
end
end
end
Write a custom rake task to migrate the data as needed
This question addresses the same issue of creating custom migrations in a mongoid setup.
Runtime changing model with mongodb/mongoid
I had the some scenario recently, where I have to do some data migration only once (basically update dirty data);
So what I did have a mongoid migrations in /db/migrate/ and override the db:migrate task so that it creates a collection in mongo db of that app itself, say "migrations", that record the migration that got fired, with that, none of the migration will run again, and you can keep adding migrations with some hierarchy (if in case migration is interdependent).
Let's say I used the following command to create a "User" model:
script/generate model User username:string
This creates the user.rb file along with the migration rb file to create the Users table. Now, I want to add an email column to my User model. What's the best way to do that? Do I do it manually and write the migration file by hand or is there a shortcut for doing it? If I write the migration by hand, do I have to name it the same way as the previous migration script (with a timestamp in front) to guarantee that it runs after the previous migration?
Don't worry about the timestamp. It will be generated automatically.
You can do a
rails generate migration add_email_to_user email:string
This would automatically create a migration file which would look like this:
class AddEmailToUser < ActiveRecord::Migration
def self.up
add_column :email, :string
end
def self.down
remove_column :email
end
end
the file would have the timestamp in the format YYYYMMDDHHMMSS (For Rails 2.1 and above) appended in front of the filename.
The Guide has information about generating migrations. If you use the rails generator, it will create correctly named files:
ruby script/generate migration AddEmailToUser email:string
Well you can do two things:
1) If you haven't deployed this anywhere yet, or you don't mind dumping the db and running your migrations again, then modify the file. Remove the tables from your db, and run db:migrate. Easy to do this in development.
2) If this app is in production, or you don't want to drop all your tables. Then create a new migration file. Then in this new migration add/modify/drop the column. Then run db:migrate and the new changes will take effect in your table. This is the best practice.
As for naming your migration, timestamps are used because rails will create a table that keeps track of the latest migrations ran. For this, it is better to use the timestamps. But if you choose, you can use your own convention instead of timestamps. Maybe name them 001_migration.rb, 002_migration.rb, etc.
Hope that helps.
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 am a Rails beginner and trying to understand how rails migration works.
I have created a scaffold like:
script/generate scaffold Item col1:string col2:text
rake db:migrate
I would like to add another col4 using migration:
I created a migration as follows:
class AddCol4 < ActiveRecord::Migration
def self.up
add_column :items, :col4, :numeric
Item.reset_column_information
end
def self.down
remove_column :items, :col4
end
end
When I run rake db:migrate the new column gets added. However the view is out of sync.
Am I supposed to manually add the new column to the view? Is there a way to auto-regenerate the model/view using the new table columns?
Sorry, it is a basic question but from my experience with other frameworks, it should have been automatic.
The rails guide on migration does not make this obvious regarding how the synchronization is supposed to work after you perform a migration.
Unfortunately you will need to modify the view manually. The view is created by running the script/generate scaffold command. Migrations only change the database. Technically, you can rerun the scaffold command and have it regenerate the view. It will ask you if you want to overwrite the previous file, however, if you go this route, you will still need to specify ALL of the columns that you want. You can't simply add some here and there.
If you are early in development, then you might take this route. Simply run
script/destroy scaffold Item
and then rerun
script generate scaffold Item col1:string col2 string col3:numeric
There are some dynamic scaffolding extensions available such as ActiveScaffold if you are creating something that only a few users will see, but I would recommend doing the HTML yourself as it will always come out the way you want.
I can't seem to find any of the other dynamic scaffolding plugins. There used to be quite a few...
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