问题描述
我是第一次使用 Active Admin 制作笔记应用程序,但我遇到了一个让我感到困惑的错误,因为关系存在,就我在架构中看到的而言,我的迁移存在。我目前有 2 个模型 Note
belongs_to
Content
和 Content
has_many
:notes
。我正在尝试为 Contents 自定义我的节目,因此它会显示不同的内容,如下所示,但我不断收到 notes
的无方法错误,此关系存在并且应该可以工作,因为它是两个表之间的外键?>
class Note < ApplicationRecord
belongs_to :content
end
class Content < ApplicationRecord
has_many :notes
end
ActiveAdmin.register Content do
permit_params :name,note_ids: []
show title: 'Test' do
h3 'You have ' + pluralize(content.notes.count,'note') + ' referencing ' + `#{content.name}`
end
ActiveRecord::Schema.define(version: 2021_05_06_140826) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "active_admin_comments",force: :cascade do |t|
t.string "namespace"
t.text "body"
t.string "resource_type"
t.bigint "resource_id"
t.string "author_type"
t.bigint "author_id"
t.datetime "created_at",precision: 6,null: false
t.datetime "updated_at",null: false
t.index ["author_type","author_id"],name: "index_active_admin_comments_on_author_type_and_author_id"
t.index ["namespace"],name: "index_active_admin_comments_on_namespace"
t.index ["resource_type","resource_id"],name: "index_active_admin_comments_on_resource_type_and_resource_id"
end
create_table "admin_users",force: :cascade do |t|
t.string "email",default: "",null: false
t.string "encrypted_password",null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at",null: false
t.index ["email"],name: "index_admin_users_on_email",unique: true
t.index ["reset_password_token"],name: "index_admin_users_on_reset_password_token",unique: true
end
create_table "contents",force: :cascade do |t|
t.string "name"
t.datetime "created_at",null: false
end
create_table "notes",force: :cascade do |t|
t.string "title"
t.text "body"
t.bigint "content_id",null: false
t.datetime "created_at",null: false
t.index ["content_id"],name: "index_notes_on_content_id"
end
add_foreign_key "notes","contents"
end
解决方法
您确定内容是 ActiveRecord 对象吗?看起来它是 ActiveSupport SafeBuffer 的实例。