将新的列events_count添加到表users
后,我收到此错误:
SQLite3::SQLException: no such column: events_count: UPDATE "users" SET "events_count" = COALESCE("events_count", 0) + 1 WHERE "users"."id" = 2
这是带有错误行的提取源:
@event.user_id = current_user.id
respond_to do |format|
**if @event.save**
format.html { redirect_to @event, notice: 'Event was successfully created.' }
format.json { render :show, status: :created, location: @event }
else
这是我的用户表:
create_table "users", force: true do |t|
.
.
.
.
t.integer "events_count", default: 0
end
这是来自users_path排名的html:
<tr>
<% User.all.each do |user| %>
<td> <%= user.name %></td>
<td> <%= user.events_count %></br></td>
<% end %>
</tr>
这是事件模型:
class Event < ActiveRecord::Base
acts_as_commontable
mount_uploader :picture, PictureUploader
acts_as_votable
belongs_to :user, dependent: :destroy,counter_cache: true
end
任何人都知道可能出现的问题?
编辑:
这是迁移文件:
class AddEventsCountToUsers < ActiveRecord::Migration
def change
add_column :users, :events_count, :integer, :default => 0
end
end
这是事件控制器:
# POST /events
# POST /events.json
def create
@event = Event.new(event_params)
@event.user_id = current_user.id
respond_to do |format|
if @event.save
format.html { redirect_to @event, notice: 'Event was successfully created.' }
format.json { render :show, status: :created, location: @event }
else
format.html { render :new }
format.json { render json: @event.errors, status: :unprocessable_entity }
end
end
end
# Never trust parameters from the scary internet, only allow the white list through.
def event_params
params.require(:event).permit(:title, :description, :picture, :start_date, :start_time, :end_time, :location, :user_id, :city)
end
end
答案 0 :(得分:0)
将用户表编辑为:
create_table "users", force: true do |t|
.
.
.
.
t.integer :events_count, default: 0
end
运行以下命令: -
rake db:drop
rake db:create
rake db:migrate
注意: - 它将删除数据库中的所有数据
答案 1 :(得分:0)
似乎您刚刚将event_counts
而非events_count
添加到已存在的create_users.rb
迁移文件中。
您必须执行查询,为额外列events_count
rails g migration AddEventsCountToUsers events_count:integer
在那之后执行rake db:migrate
或强>
执行rake db:rollback
,在create_users.rb
迁移文件中添加该列并执行rake db:migrate
如评论中所述,请尝试运行rake db:migrate:up VERSION=20140511122817