Rake aborted错误,无法迁移。有什么想法吗?

时间:2015-09-07 06:01:18

标签: ruby-on-rails ruby migration

这是我在控制台中获得的堆栈跟踪错误。我还需要补充一点,这不起作用“rake db:migrate RAILS_ENV = development”。

    activerecord (4.2.0) lib/active_record/migration.rb:393:in `check_pending!'
    activerecord (4.2.0) lib/active_record/migration.rb:374:in `call'
    actionpack (4.2.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
    activesupport (4.2.0) lib/active_support/callbacks.rb:88:in `call'
    activesupport (4.2.0) lib/active_support/callbacks.rb:88:in `_run_callbacks'
    activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_call_callbacks'
    activesupport (4.2.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
    actionpack (4.2.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
    actionpack (4.2.0) lib/action_dispatch/middleware/reloader.rb:73:in `call'
    actionpack (4.2.0) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
    actionpack (4.2.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
    web-console (2.2.1) lib/web_console/middleware.rb:39:in `call'
   actionpack (4.2.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
    railties (4.2.0) lib/rails/rack/logger.rb:38:in `call_app'
    railties (4.2.0) lib/rails/rack/logger.rb:20:in `block in call'
    activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `block in tagged'
    activesupport (4.2.0) lib/active_support/tagged_logging.rb:26:in `tagged'
    activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `tagged'
    railties (4.2.0) lib/rails/rack/logger.rb:20:in `call'
    actionpack (4.2.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
    rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
    rack (1.6.4) lib/rack/runtime.rb:18:in `call'
    activesupport (4.2.0)     lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
    rack (1.6.4) lib/rack/lock.rb:17:in `call'
    actionpack (4.2.0) lib/action_dispatch/middleware/static.rb:113:in `call'
    rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
    railties (4.2.0) lib/rails/engine.rb:518:in `call'
    railties (4.2.0) lib/rails/application.rb:164:in `call'
    rack (1.6.4) lib/rack/lock.rb:17:in `call'
    rack (1.6.4) lib/rack/content_length.rb:15:in `call'
    rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
    C:/row/ruby200/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
    C:/row/ruby200/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
    C:/row/ruby200/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'

这是我打开服务器并尝试访问我的网站时出现的错误,

ActiveRecord::PendingMigrationError

Migrations are pending. To resolve this issue, run: bin/rake db:migrate RAILS_ENV=development 

这是我的迁移文件,

class CreateFriendships < ActiveRecord::Migration
  def change
    create_table :friendships do |t|
        class CreateFriendships
            def self.up
                create_table :friendships do |t|
                t.integer :user_id, :friend_id
                t.string :status
                t.timestamps
            end
            end
            def self.down
                drop_table :friendships
            end
        end

      t.timestamps null: false
    end
  end
end

如果您可能需要查看其他任何代码,请告诉我们!我会更新我的问题!

编辑:这是我的welcome_controller.rb,

class WelcomeController < ApplicationController
  def welcome
    @user = User.new 
  end
end

编辑:这是我得到的以下错误

ArgumentError in WelcomeController#welcome

Unknown key: :conditions. Valid keys are: :class_name, :class, :foreign_key, :validate, :autosave, :table_name, :before_add, :after_add, :before_remove, :after_remove, :extend, :primary_key, :dependent, :as, :through, :source, :source_type, :inverse_of, :counter_cache, :join_table, :foreign_type

编辑:这是我的用户模型,

class User < ActiveRecord::Base
    has_secure_password
    validates :first_name, :last_name, :email, presence: true, uniqueness: true 
    validates_inclusion_of :age, in: 10..100
    validates :password, presence: true, length: { minimum: 4 }, allow_nil: true
    has_many :posts
    has_many :friends, :through => :friendships, :conditions => "status = 'accepted'"
    has_many :requested_friends, :through => :friendships, :source => :friend, :conditions => "status = 'requested'", :order => "friendships.created_at"
    has_many :pending_friends, :through => :friendships, :source => :friend, :conditions => "status = 'pending'", :order => "friendships.created_at"
    has_many :friendships, :dependent => :destroy
    has_attached_file :profile_picture, :styles => { :medium => "300x300>", :thumb => "100x100>" }, 
:default_url => "app/assets/images/missing.png", 
:path => ":rails_root/public/system/:class/:attachment/:id_partition/:style/:filename" 
    validates_attachment_content_type :profile_picture, :content_type => /\Aimage\/.*\Z/
end

1 个答案:

答案 0 :(得分:1)

我认为你需要像这样更改你的迁移文件,

class CreateFriendships < ActiveRecord::Migration
 def change
   create_table :friendships do |t|
      t.integer :user_id, :friend_id
      t.string :status
      t.timestamps null: false
   end
 end
end

class CreateFriendships < ActiveRecord::Migration
  def self.up
    create_table :friendships do |t|
      t.integer :user_id, :friend_id
      t.string :status
      t.timestamps
   end
  end

  def self.down
    drop_table :friendships
  end
end

然后在运行服务器之前使用rake db:migrate

在Rails 4中,条件是通过范围块实现的。所以你可以尝试一下。

has_many :friends, -> { where "status = 'accepted'" }, :through => :friendships

与此相同,您必须更改携带:conditions的所有关系。 requested_friends和pending_friends。