How to use Rails Migrations - Part II
April 26th, 2007 by Yavor Ivanov

This article is part two of the series about Rails Migrations and it will explain on a deeper level how to use the Ruby on Rails migrations.
After we spoke about Migrations in our previous article found here we understood the basics needed to use Migrations in Ruby on Rails.
Now to begin with the code and something we did not mention earlier.
You see indexes are something that a good database design MUST contain!
This example shows how to add and remove an indexed field.
And now let’s take a look at some examples which might or might not be so obvious to make.
We explained the basics in Part 1 and 2 till now. We should be able to build our database almost perfectly but as all humans we are not perfect and we might want to change someday the type of a column. It might not satisfy us to migrate back and forward so we can make this:
But sadly in real life there might be a table in which we have sensitive data and we don’t want to loose it due to changing of types or some other reason. Of course Rails has the answer.
As we define this special exception in self.down we throw an exception and we prevent the migration from being reversed to a previous state. And now for some really advanced things.
Options for Creating Tables
As we know we can pass certain options to everything in Ruby on Rails. Migrations are not exception to this rule. These are some tricky things which might help us in particular situations:
:force => true This will force the migration to drop the table with the desired name and create the new one. This is really useful if we want to create migrations that force our database into a certain known state. Of course this might drop important data too.
:temporary => true This option creates a temporary table. This is a bit pointless in a migration,but it has it’s usage elsewhere.
This option is a powerful one. It allows you to pass parameters to your database like real SQL. These parameters are added to the end of the CREATE TABLE statement. The following example defines that the primary key (by default ID) will be auto incremented with initial value of 10000.
Well as our part 2 of How to use migrations in Rails focus more on particular cases here is another one. By default Rails will make ID the Primary Key without telling anything for it in the migration. In some cases we might want to use a different Primary Key. Here is how to define one:
And here is one more cool thing! If you ever hear someone arguing with your teacher at university that Primary Keys shouldn’t be anything else than Integers, and you think this is right. Well it is. And in Rails you can’t do this. You can always hack around it but it is build with the assumption that it will do better if you follow the standards. So Primary Keys are always Integers.
And as final words I want to show you that any limitations you feel can be overcome by making use of standard plain SQL. Here is how to use Native SQL and directly pass it into the migration using EXECUTE.
notice: using a native SQL might loose our database independence as you might code it for a specific database engine.
This is for now! Hope I gave more light on migrations. In the third part we will discus even more advanced topics like how to load data from a file, how to update certain data and some other cool things.
You like it? Digg it!
Tags: code, development, migrations, tutorial
21 Responses to “How to use Rails Migrations - Part II”
Leave a Reply
April 26th, 2007 at 8:32 pm
Thanks for this fresh new tutorial! Explanation is very thorough, and accurate!
August 29th, 2007 at 8:14 pm
Hello,
i was working on rails for past 15 days and this post is very helpful and got idea about migration.hw about part III of this site.Is it available?
Thanks
Ayyappan K.
August 29th, 2007 at 8:23 pm
Well thanks
YES! There will be a third article in the migration series which we will came up with. Actually Stanislav will have ideas too this time. We got plenty of experience with http://svejo.net project which is being redesigned and redeveloped. It is a local bulgarian Digg clone. So we will came up with some fresh new articles/tutorials when time is available 


If you have any trouble with rails try our forum. We take care of the site as support for the people so we will help
Take care ruby guys
August 29th, 2007 at 9:39 pm
hello Yavor Ivanov ,
what kind of proj is ruby best suit?its given that we can develop ebay using rails.can i get more details regarding this.]
Thanks
Ayyappan K.
August 29th, 2007 at 9:45 pm
I did not quite get that question but as a whole my opinion is that every web language can perform good on any web project if you choose your tools right. Actually I use Rails for web only … I mean no hard coder on Ruby
ask again in more details… if you want you can ask in our forum so we don’t comment off-topic here. Cheers!
August 29th, 2007 at 9:55 pm
hi,
If we have an application and in position to choose the platform/framework like ruby on rails,java,dot net …… then For what kind of application u choose rails.
Thanks
Ayyappan K.
August 29th, 2007 at 10:05 pm
It is a topic to be a flame war but my opinion is that Rails can be used from small to big projects. Some people would disagree with me on that cause Rails has some performance issues but really if you are not google and you have financial stability you can always extend your app on multiple machines and Rails is made very easy for programmers and admins to extend on multiple servers and so on… so I stand for my word and say: “You can do everything with RUBY ON RAILS”. The power of rails is that it is agile so the type of project would be some small to big project which has not much clarity in what it would become… so there Rails will kick ass!
August 29th, 2007 at 10:16 pm
hi,
can u tell me some projects that are done using rails, Does rails support real time proj like airlines…
Thanks,
Ayyappan K.
August 29th, 2007 at 10:23 pm
You may want to look at our blog carnivals -> http://rubycorner.net/category/rails-blog-carnival/
August 29th, 2007 at 10:27 pm
hi Yavor Ivanov ,
can i get to know about Why we r moving to Rails? i got many ans as Fater developmnent,Security,…. but i dont get justification for those? Can u provide information regarding this.
Thanks
Ayyappan K.
August 29th, 2007 at 10:40 pm
Think that will answer most of your questions
http://www.railsenvy.com/2007/3/22/ruby-on-rails-video-presentation
September 4th, 2007 at 7:15 pm
hello,
any one tell me about the usage of flash[:notice] in ruby
the code below
def create
@account=Account.new(params[:account])
if @account.save
flash[:notice]=’Account created’#2
redirect_to :action => ‘list’
else
render :action =>’new’
end
end
in above coding, i cant get the line marked as #2.
can any one explain me that.
thanks
Ayyappan K.
September 4th, 2007 at 7:19 pm
Not the best place to ask man!
The people here have forum… try there. And explain well what you are trying to do.
September 4th, 2007 at 7:22 pm
i posted already in http://www.rubylearning.com/forum/viewtopic.php?p=288#288 and i didnt get response,can u suggest me any other forum.
September 4th, 2007 at 7:24 pm
Post it @ forum.rubycorner.net pretty sure you will get the answer!
September 4th, 2007 at 7:42 pm
Thank You!
September 7th, 2007 at 8:20 pm
hello,
How to retrieve the datas that we enter in text box … in controller. suggest me any site regarding this.
Thanks
Ayyapan k.
September 14th, 2007 at 10:56 pm
hello,
i retrieved a value from db using max function.i just want that value to be incremented by 1 and assigned to the textbox in rhtml page.Actually i have a table named registration and 4 records. the accno field is one i retrieved using accno = login.find_by_sql ’select max(accno) from logins’.when i print accno i got value as #.the actual value in accno = 104. hw to get that value.help me to accomplish this.
Thanks
Ayyappan K
October 15th, 2007 at 4:31 pm
Ayyappan K
try this
October 15th, 2007 at 4:32 pm
ruby code
q=Item.find_by_sql(”select max(dad) as a from items”)
q.each do |a|
=”#{a.a}”
end
November 2nd, 2007 at 11:14 pm
Excellent series of articles. Well-written.