English 中文(简体)
rake db:migrate running all migrations correctly
原标题:

I m fairly new to Ruby on Rails here.

I have 2 migrate files that were provided. The first one, prefixed with 001, creates a table and some columns for that table. The next migrate file, prefixed with 002, inserts rows into the table created in file 001.

Running the migration (rake db:migrate in command line) correctly creates the table but doesn t insert any of the data which is the problem. The code from the insertion looks like this (except with a lot more Student.create statements,

class AddStudentData < ActiveRecord::Migration
  def self.up
        ...
   Student.create(:name => "Yhi, Manfredo", :gender => "M")
        ...
  end

  def self.down
    Student.delete_all
  end
end

My understanding is that Student is a model object, so my Student model looks like this,

class Student < ActiveRecord::Base
end

Do I need to explicitly define a create method in Student or is that something that s given? (This project was made using scaffold)

Thanks.

Edit 1: I used Damien s suggestion and called create! instead of create but got the same response. Then what I did to see whether the code was even reaching that far was call this,

Student.create12312313!(:name => "foo", :gender => "M")

which is obviously invalid code and the migrate didn t throw any error.

Edit2: Answer found. The schema_migrations table had its version set to 3, and I only had 3 different migration files so it never ran any of the migration files I had. That s why nothing was ever updating, and the bogus creates I used were never throwing errors. The reason the student data wasn t inserted the first time was because a certain table was already in the database and it caused a conflict the first time I migrated. So what I was really looking for wasn t db:migrate but rather db:reset Several hours well spent.

最佳回答

The create method is inherited from ActiveRecord::Base.
So no, you don t need to define it.

One reason why your datas could not be included would be that you have validations that doesn t pass.
You can easily see the error making your datas not being included by using create! instead of create.
So if the model can t be created, an exception will be thrown and the migrations will fail.

问题回答

You may want to look at Data Seeding in rails 2.3.4. And is your rails migrations really running 001_create_whatever.rb? or were you just using that as an example? since 2.2.2 (iirc) migrations have been using timestamps such as 10092009....create_whatever.rb

How old is your rails version?

The migrations won t run if their schema number is in the database.

For older versions of rails, there will be a single row with the highest migration performed in it.

For newer versions, every migration gets a unique time-stamp as its version number, and its own row in schema_migrations when it gets added.





相关问题
Copy data from Access to SQL

I am in the process of migrating an existing Access database to a SQL database with a web front-end. I have successfully copied the database to SQL using the SQL Server Migration tool and am working ...

MongoMapper and migrations

I m building a Rails application using MongoDB as the back-end and MongoMapper as the ORM tool. Suppose in version 1, I define the following model: class SomeModel include MongoMapper::Document ...

Switching to WPF. Is it time?

I m considering switching from MFC to WPF. My first concern is that there are too many users who don t have .NET with WPF installed yet. Can anybody point to a source containing the WPF penetration ...

rake db:migrate running all migrations correctly

I m fairly new to Ruby on Rails here. I have 2 migrate files that were provided. The first one, prefixed with 001, creates a table and some columns for that table. The next migrate file, prefixed ...

Migrate Java Applet to what/where?

I am reviewing currently a medium size code base (around 30K LOC) which uses a huge Applet and interfaces with other systems. It s a tool to create custom labels, so we need drag-n-drop and other ...

热门标签