Having some questions about migrations

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

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.

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!

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!

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. :wink:

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. :smiley:

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

cheers,
sameera

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
[email protected]