Ruby on rails Mysql2::错误:列存在时未知列活动管理员注册模型
更新 我只是注意到我的collections表实际上没有用户id。我刚刚加了Ruby on rails Mysql2::错误:列存在时未知列活动管理员注册模型,ruby-on-rails,Ruby On Rails,更新 我只是注意到我的collections表实际上没有用户id。我刚刚加了 create_table "collections", force: true do |t| t.integer "collectable_id" t.string "collectable_type" t.string "title" t.string "description" t.datetime "created_at" t.datetime "
create_table "collections", force: true do |t|
t.integer "collectable_id"
t.string "collectable_type"
t.string "title"
t.string "description"
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "team"
t.integer "user_id"
end
但它仍然给我一个错误
使用控制器更新
class CollectionsController < ApplicationController
before_filter :require_user, except: [:index, :show]
def index
if params[:user_id]
@collections = User.find(params[:user_id]).collections
elsif params[:pic_id]
@pic = Pic.find(params[:pic_id])
@collections = current_user.collections
render partial: 'modal', layout: false
else
@collections = current_user.collections
end
end
def edit
@collection = Collection.find params[:id]
end
def update
@collection = Collection.find params[:id]
if @collection.update_attributes(collection_params)
redirect_to collections_path
else
render 'edit'
end
end
def create
@collection = Collection.new(collection_params)
@collection.collectable = current_user
if @collection.save
@pic = @collection.pics.first
@collections = current_user.collections
render partial: 'modal', layout: false
end
end
def destroy
@collection = Collection.find(params[:id])
if current_user.collections.include? @collection or current_user.admin?
@collection.destroy
else
flash[:alert] = 'You do not have permission to delete this inspiration collection'
end
redirect_to collections_path
end
def show
@collection = Collection.find params[:id]
@brand = Brand.find(params[:from_brand_id]) if params[:from_brand_id]
end
def remove_pic
@collection = Collection.find params[:id]
if @collection.collectable_type == 'User' and current_user.id == @collection.collectable_id
@collection.pic_relationships.where(pic_id: params[:pic_id]).each{|pic_relationship| pic_relationship.destroy}
end
@pic = Pic.find params[:pic_id]
@collections = current_user.collections
render partial: 'collections/modal', layout: false
end
private
def collection_params
params.require(:collection).permit(:title, pic_relationships_attributes: [:id, :pic_id, :_destroy])
end
end
我使用的是rails 4,所以我允许在我的app/admin/collection模型中使用params
ActiveAdmin.register Collection do
permit_params :collectable_id, :collectable_type, :title, :description, :created_at, :updated_at, :team
# See permitted parameters documentation:
# https://github.com/activeadmin/activeadmin/blob/master/docs/2-resource-customization.md#setting-up-strong-parameters
#
# permit_params :list, :of, :attributes, :on, :model
#
# or
#
# permit_params do
# permitted = [:permitted, :attributes]
# permitted << :other if resource.something?
# permitted
# end
end
当我走到我的管理面板,点击记录下的编辑
我明白了
ActiveRecord::StatementInvalid at /admin/collections/3/edit
Mysql2::Error: Unknown column 'collections.collectable_type' in 'where clause': SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 AND (`collections`.`collectable_type` = 'User') ORDER BY `users`.`id` ASC LIMIT 1
可收集类型的值为user,单击view时,我可以在管理仪表板中看到它。数据存在,为什么会说缺少列?您的查询如下所示
SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 AND (`collections`.`collectable_type` = 'User') ORDER BY `users`.`id` ASC LIMIT 1
如果仔细查看查询,您将从users表中进行选择。users表不知道有关collections.collectable\u类型的信息
您应该将查询集合与用户联接起来
SELECT `users`.* FROM `users` join collections on collections.collectable_id = users.id WHERE `users`.`id` = 1 AND (`collections`.`collectable_type` = 'User') ORDER BY `users`.`id` ASC LIMIT 1
发布您的收藏控制器我在active admin中没有控制器。我仅注册集合模型/表格。我不知道它为什么通过用户表运行sql查询。
SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 AND (`collections`.`collectable_type` = 'User') ORDER BY `users`.`id` ASC LIMIT 1
SELECT `users`.* FROM `users` join collections on collections.collectable_id = users.id WHERE `users`.`id` = 1 AND (`collections`.`collectable_type` = 'User') ORDER BY `users`.`id` ASC LIMIT 1