English 中文(简体)
铁路,SQL例外:无此类列
原标题:Rails, SQLException: no such column

重新检查代码后, 重新启动我的电脑, 运行 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
最佳回答

一旦移民开始流动,如果你更新它,铁路就会重新运行(它不知道你做了什么改变)。

要么在编辑前滚回迁移(rake db:rollb back),要么创建新的迁移,添加额外的列。

问题回答

迁移可能会失败, 如果您在迁移上有一些错误, 例如它无法工作 。 请确认它已经运行, 您可以检查您的 sqliite db, 查看是否有用户身份( id) 。





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签