您好我的控制器中有此功能
def destroy
fallower = Fallower.where(user_id: 1, author_id: 2).first
fallower.destroy
respond_to do |format|
format.js
end
end
它抛出了我的错误
ActiveRecord::StatementInvalid in FallowersController#destroy
PG::SyntaxError: ERROR: zero-length delimited identifier at or near """"
LINE 1: DELETE FROM "fallowers" WHERE "fallowers"."" = $
fallower model
class Fallower < ActiveRecord::Base
belongs_to :user
end
fallowers table
class CreateFallowers < ActiveRecord::Migration
def change
create_table :fallowers, id: false do |t|
t.belongs_to :user
t.integer :author_id
t.timestamps null: false
end
end
end
任何想法为什么正常的破坏功能这次不起作用?
答案 0 :(得分:0)
您无法销毁记录,因为它们在架构中没有ID列,而destroy
方法在ID(默认为主键)列上工作。
您可以通过两种方式解决此问题:
1)您可以在表架构中添加任何其他主键,并在模型中设置它,如下所示。但是此选项不会影响您之前添加的记录。
class Fallower < ActiveRecord::Base
primary_key :xxx_id
belongs_to :user
end
2)您可以使用条件调用delete_all
方法,因为它不会先初始化对象。更多细节here。
def destroy
Fallower.delete_all(user_id: 1, author_id: 2)
respond_to do |format|
format.js
end
end