开发者

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(:开发者_JAVA技巧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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜