重新检查代码后, 重新启动我的电脑, 运行 Rake db: miggrate, 我不知道还能用它做什么。 我不断收到以下错误 :
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 %>
这和这个家族有什么关系吗?
< 加强> 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>
< 强 > 上载.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
< 强 > 用户.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-zd-.]+.[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
来自用户_ controller.rb < 强 > 的 < 强 > 。
def show
@user = User.find(params[:id])
@uploads = @user.uploads.paginate(page: params[:page])
end
<% 1> > 201200524145724_ 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