我希望使用Nil命令删除两个表之间的关联。
主题模型看起来像subject.rb
class Subject < ApplicationRecord
has_one :page
# subject has one page
# scope :visible, -> { where(visible: true) }
# scope :invisible, -> { where(visible: false) }
# scope :sorted, -> { order("position ASC") }
# scope :newest_first, -> { order("created_at DESC") }
scope :visible, lambda { where(visible: true) }
scope :invisible, lambda { where(visible: false) }
scope :sorted, lambda { order("position ASC") }
scope :newest_first, lambda { order("created_at DESC") }
scope :search, lambda { |query| where(['name LIKE ?', "%#{query}%"]) }end Page模型如下
page.rb
class Page < ApplicationRecord
# foreign key
belongs_to :subject
end页面迁移的文件看起来像
20210919102818_create_pages.rb
class CreatePages < ActiveRecord::Migration[6.1]
def up
create_table :pages do |t|
t.integer "subject_id"
t.string "name"
t.string "permalink"
t.integer "position"
t.boolean "visible", default:false
t.timestamps
end
add_index :pages, :subject_id
add_index :pages, :permalink
end
def down
drop_table :pages
end
end 主题迁移文件看起来像
20210919102807_create_subjects.rb
class CreatePages < ActiveRecord::Migration[6.1]
def up
create_table :pages do |t|
t.integer "subject_id"
t.string "name"
t.string "permalink"
t.integer "position"
t.boolean "visible", default:false
t.timestamps
end
add_index :pages, :subject_id
add_index :pages, :permalink
end
def down
drop_table :pages
end
end 使用零的错误部分。它显示了
ActiveRecord::RecordNotSaved (未能删除现有关联页)。在外键设置为零后,记录无法保存。)
误差图

https://stackoverflow.com/questions/69264398
复制相似问题