コードを再確認し、コンピューターを再起動し、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