重新检查代码,重新启动计算机,然后运行rake db:migrate,我不知道还有什么可以解决这个问题。我一直收到以下错误:
SQLite3::SQLException: no such column: uploads.user_id: SELECT COUNT(*) FROM "uploads" WHERE "uploads"."user_id" = 1
Extracted source (around line #12):
9: </section>
10: </aside>
11: <div class="span8">
12: <% if @user.uploads.any? %>
13: <h3>Uploads (<%= @user.uploads.count %>)</h3>
14: <ol class="uploads">
15: <%= render @uploads %>
这与has_many关联有关吗?
show.html.erb
<% provide(:title, @user.name) %>
<div class="row">
<aside class="span4">
<section>
<h1>
<%= gravatar_for @user %>
<%= @user.name %>
</h1>
</section>
</aside>
<div class="span8">
<% if @user.uploads.any? %>
<h3>Uploads (<%= @user.uploads.count %>)</h3>
<ol class="uploads">
<%= render @uploads %>
</ol>
<%= will_paginate @uploads %>
<% end %>
</div>
</div>
upload.rb
class Upload < ActiveRecord::Base
attr_accessible :title
belongs_to :user
validates :title, presence: true
validates :user_id, presence: true
default_scope order: 'uploads.created_at DESC'
end
user.rb
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
has_secure_password
has_many :uploads, dependent: :destroy
before_save { |user| user.email = email.downcase }
before_save :create_remember_token
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true,
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
validates :password, length: { minimum: 6 }
validates :password_confirmation, presence: true
private
def create_remember_token
self.remember_token = SecureRandom.urlsafe_base64
end
end
来自users_controller.rb的
def show
@user = User.find(params[:id])
@uploads = @user.uploads.paginate(page: params[:page])
end
20120524145724_create_uploads.rb
class CreateUploads < ActiveRecord::Migration
def self.up
create_table :uploads do |t|
t.string :title
t.integer :user_id
t.timestamps
end
add_index :uploads, [:user_id, :created_at]
end
def self.down
drop_table :uploads
end
end
答案 0 :(得分:3)
运行迁移后,如果您更新它,rails将不会重新运行它(它不知道您所做的更改)。
在编辑之前滚动迁移(rake db:rollback)或创建添加额外列的新迁移。
答案 1 :(得分:0)
迁移可能会失败一些,如果你有一些错误,例如它不会工作。确保它确实运行,你可以检查你的sqlite数据库,看看是否有user_id