# Having some questions about migrations

**URL:** <https://www.ruby-forum.com/t/having-some-questions-about-migrations/179546>\
**Category:** Rails\
**Created:** [November 27, 2009, 11:48pm UTC](https://www.ruby-forum.com/t/having-some-questions-about-migrations/179546 "2009-11-27T23:48:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexanderl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f0a364/32.png) [@alexanderl](https://www.ruby-forum.com/u/alexanderl)\
**Post date:** [November 27, 2009, 11:48pm UTC](https://www.ruby-forum.com/t/having-some-questions-about-migrations/179546/1 "2009-11-27T23:48:58Z")

</div>

Hello! I have just started ruby on rails and i think it’s really funny.  
At the moment i am building a project just for fun, where i can post  
links to different websites, in different categories.

For example i can add a little link and description for “Ruby-forum” and  
place it under the category “Programming”. So far so good!  
But now i want to add a feature where other people (Well… Me :D) can  
suggest a webpage. Then they should fill out a simple form and click  
“Send”, and the suggestion will be puted in a table.

So, at the moment i have two tables in my database, “reviews” and  
“categories”. Everything works fine, i can post/edit/delete/show/show  
categories etc. But how am i going to do if i want to add another  
function that uses another table than those two?

For example a form that puts information in a table called  
“suggestions”. I would really like some tips on this! I guess i have to  
migrate the table with the others, create the model and put the function  
in the controller? But i am not sure how to start…

Regards  
Alexander LJ

---

<div class="post-metadata">

**Author:** ![alexanderl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f0a364/32.png) [@alexanderl](https://www.ruby-forum.com/u/alexanderl)\
**Post date:** [November 28, 2009, 1:14am UTC](https://www.ruby-forum.com/t/having-some-questions-about-migrations/179546/2 "2009-11-28T01:14:48Z")

</div>

Alexander L. wrote:

> Hello! I have just started ruby on rails and i think it’s really funny.  
> At the moment i am building a project just for fun, where i can post  
> links to different websites, in different categories.
> 
> For example i can add a little link and description for “Ruby-forum” and  
> place it under the category “Programming”. So far so good!  
> But now i want to add a feature where other people (Well… Me :D) can  
> suggest a webpage. Then they should fill out a simple form and click  
> “Send”, and the suggestion will be puted in a table.
> 
> So, at the moment i have two tables in my database, “reviews” and  
> “categories”. Everything works fine, i can post/edit/delete/show/show  
> categories etc. But how am i going to do if i want to add another  
> function that uses another table than those two?
> 
> For example a form that puts information in a table called  
> “suggestions”. I would really like some tips on this! I guess i have to  
> migrate the table with the others, create the model and put the function  
> in the controller? But i am not sure how to start…
> 
> Regards  
> Alexander LJ

I, too, am new.

Basically, you create a new migration.

I believe yo do something like …

ruby script/generate scaffold newtable somevariable1:string  
somevariable2:string

That create the “template” for a migration … but does not actually add  
to the database yet.

To actually create the new table … you then do

rake db:migrate

the rake command will magically do the new migration.

---

<div class="post-metadata">

**Author:** ![alexanderl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f0a364/32.png) [@alexanderl](https://www.ruby-forum.com/u/alexanderl)\
**Post date:** [November 28, 2009, 1:29am UTC](https://www.ruby-forum.com/t/having-some-questions-about-migrations/179546/3 "2009-11-28T01:29:16Z")

</div>

Ralph S. wrote:

> I, too, am new.
> 
> Basically, you create a new migration.
> 
> I believe yo do something like …

Are you really sure scaffold is the correct method?

> ruby script/generate scaffold newtable somevariable1:string  
> somevariable2:string
> 
> That create the “template” for a migration … but does not actually add  
> to the database yet.
> 
> To actually create the new table … you then do

There is a problem there, how can i run the migration again when 2 of 3  
tables already exists? Then the migration will fail. The only option is  
if i delete all the tables in my database, then run the migration and  
let it generate them again. But then the information will be lost. :\<

> rake db:migrate
> 
> the rake command will magically do the new migration.

Thanks for the answers!

---

<div class="post-metadata">

**Author:** ![alexanderl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f0a364/32.png) [@alexanderl](https://www.ruby-forum.com/u/alexanderl)\
**Post date:** [November 28, 2009, 2:16am UTC](https://www.ruby-forum.com/t/having-some-questions-about-migrations/179546/4 "2009-11-28T02:16:43Z")

</div>

Alexander L. wrote:

> Ralph S. wrote:
> 
> > I, too, am new.
> > 
> > Basically, you create a new migration.
> > 
> > I believe yo do something like …
> 
> Are you really sure scaffold is the correct method?

I am 60% sure.

> > ruby script/generate scaffold newtable somevariable1:string  
> > somevariable2:string
> > 
> > That create the “template” for a migration … but does not actually add  
> > to the database yet.
> > 
> > To actually create the new table … you then do
> 
> There is a problem there, how can i run the migration again when 2 of 3  
> tables already exists? Then the migration will fail. The only option is  
> if i delete all the tables in my database, then run the migration and  
> let it generate them again. But then the information will be lost. :\<

No … these migrations are pretty sophisticated and this will allow you  
to add your table … and even unwind what you did.

It’s almost like a version control system for database schema.

> > rake db:migrate
> > 
> > the rake command will magically do the new migration.
> 
> Thanks for the answers!

---

<div class="post-metadata">

**Author:** ![alexanderl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f0a364/32.png) [@alexanderl](https://www.ruby-forum.com/u/alexanderl)\
**Post date:** [November 28, 2009, 2:27am UTC](https://www.ruby-forum.com/t/having-some-questions-about-migrations/179546/5 "2009-11-28T02:27:26Z")

</div>

Yes, i solved it on my own! Damn, i’ve been stuck with this shit for 4  
hours. But you had right, i could migrate the db again without having to  
loose any data. But i did not use scaffold-method thought. 😉

I solved it this way.  
First in the migration-file i putet this.

==========================================  
class Tips \< ActiveRecord::Migration  
def self.up  
create\_table :tips do |t|  
t.column :title, :string, :limit =\> 32, :null =\> false  
t.column :url, :text  
t.column :category\_id, :integer  
t.column :description, :text  
t.column :created\_by, :text  
t.column :created\_at, :timestamp  
end  
end

# def self.down drop\_table :tips end end

And then i made a model, and i already had a controller handling alot of  
stuff, so i added this code into the modell.

# ========================================== def tipus @tip = Tip.new @categories = Category.find(:all) end def addtip @tip = Tip.new(params[:tip]) if @tip.save redirect\_to :action =\> ‘success’ else @categories = Genre.find(:all) render :action =\> ‘new’ end end

# And then finaly, the fields where people tip me. “tipus.rhtml”

\<%= form\_tag :action =\> ‘addtip’ %\>

Title: \<%= text\_field 'tip', 'title' %\>

Category: \<%= collection\_select(:tip,:category\_id,@categories, :id, :name) %\>

URL: \<%= text\_field 'tip', 'url' %\>

Short description  
\<%= text\_area 'tip', 'description' %\>

\<%= submit\_tag "Create" %\> \<%= link\_to 'Back', {:action =\> 'list'} %\> ============================================

Everything works perfect, woho. =D Damn, i love rails even more now. I  
love this “Yey!”-feeling. 😃

---

<div class="post-metadata">

**Author:** ![alexanderl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f0a364/32.png) [@alexanderl](https://www.ruby-forum.com/u/alexanderl)\
**Post date:** [November 29, 2009, 6:47pm UTC](https://www.ruby-forum.com/t/having-some-questions-about-migrations/179546/6 "2009-11-29T18:47:08Z")

</div>

Hi Alexander ,

In Rails when you are creating new migations you dont want to worry  
about running them and loosing old data,

In your rails app db/schema.rb file keep a track of created tables and  
make sure there are not overriding

and in database also you have a table called schema\_migrations which  
will keep a track of all the executed migrations,

even if you want to cancel a migration you can rollback

ex : for immediate previous

rake db:rollback

here is a useful like

> **[Active Record Migrations — Ruby on Rails Guides](https://edgeguides.rubyonrails.org/active_record_migrations.html)**
>
> Active Record MigrationsMigrations are a feature of Active Record that allows you to evolve your database schema over time. Rather than write schema modifications in pure SQL, migrations allow you to use a Ruby DSL to describe changes to your...

cheers,  
sameera

---

<div class="post-metadata">

**Author:** ![alexanderl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f0a364/32.png) [@alexanderl](https://www.ruby-forum.com/u/alexanderl)\
**Post date:** [November 29, 2009, 6:54pm UTC](https://www.ruby-forum.com/t/having-some-questions-about-migrations/179546/7 "2009-11-29T18:54:05Z")

</div>

Ralph S. wrote:

> Alexander L. wrote:
> 
> > Ralph S. wrote:
> > 
> > > I, too, am new.
> > > 
> > > Basically, you create a new migration.
> > > 
> > > I believe yo do something like …
> > 
> > Are you really sure scaffold is the correct method?
> 
> I am 60% sure.

And 95% wrong. script/generate scaffold will create a migration, but it  
will also create other files. If you don’t want those other files, use  
script/generate migration.

Don’t rely on scaffolding for anything serious.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
