提问人:Ese10 提问时间:7/19/2022 最后编辑:Ese10 更新时间:7/21/2022 访问量:355
Ruby on rails - 如何与生成的文件夹关联,这些文件夹是其他文件夹的嵌套/子文件夹
Ruby on rails - How to associate with generated folders that are nested/sub folders of other folders
问:
我有一个名为 admin 的文件夹,其中有一个生成的名为 products 的脚手架,该脚手架的primary_key id 也更改为 ect。然后,我创建了一个名为 cart_products 的模型,该模型具有 .当我尝试使用它时,例如:,它会抛出一个名称错误,说belongs_to :product
@cart.cart_products.create(product: @product, quantity:)
Rails 找不到有效的产品关联模型。请在协会声明中提供 :class_name 选项。如果已提供 :class_name,请确保它是 ActiveRecord::Base 子类。
因此,我随后更改了产品型号名称的belongs_to。现在我得到了一个belongs_to :product, :class_name => "Admin::Product"
ActiveRecord::StatementInvalid - SQLite3::SQLException:没有这样的表:main.products
当我的数据库中它保存为时,它是从哪里来的?main.products
create_table "admin_products", primary_key: "ect", force: :cascade do |t|
这是我的代码的样子:
# controllers/home/cart_controller.rb
class Home::CartController < HomeController
def add
@product = Admin::Product.find_by(ect: params[:ect])
# Code breaks on next line
@cart.cart_products.create(product: @product, quantity:)
end
end
# models/cart_product.rb
class CartProduct < ApplicationRecord
belongs_to :product, class_name: "Admin::Product"
belongs_to :cart
end
# models/admin/product.rb
class Admin::Product < ApplicationRecord
has_many :cart_products
has_many :carts, through: :cart_products
end
end
# models/admin.rb
module Admin
def self.table_name_prefix
"admin_"
end
end
我尝试访问的数据库是:
# associated with models/admin/product.rb
create_table "admin_products", primary_key: "ect", force: :cascade do |t|
t.string "title"
t.decimal "price"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
# associated with models/cart_product.rb
class CreateCartProducts < ActiveRecord::Migration[7.0]
def change
create_table :cart_products do |t|
t.belongs_to :product, null: false, foreign_key: true
t.belongs_to :cart, null: false, foreign_key: true
t.integer :quantity
t.timestamps
end
end
end
答:
始终在使用任何型号的脚手架后运行
rails db:迁移
您的模型文件 admin/product.rb
class Admin::Product < ApplicationRecord
如果是,那么您需要与以下关联class_name
belongs_to :product, class_name: "Admin::Product"
评论
belongs_to :product, class_name: "Admin::Product"
admin_products
你必须告诉 rails 表名:
# app/models/admin/product.rb
module Admin
class Product < ApplicationRecord
self.table_name = "admin_products"
end
end
或者在管理模块中为模型的每个表添加前缀。
# app/models/admin.rb
module Admin
def self.table_name_prefix
"admin_"
end
end
更新
回滚迁移并更新它以修复外键约束:CreateCartProducts
# NOTE: by default when foreign key constraint is created
# the name of the foreign table is inferred from
# the argument `:product`. There is no `products` table,
# which is why SQLite complains about it in the error;
# custom primary key has to be also specified.
# t.belongs_to :product, null: false, foreign_key: true
t.belongs_to :product, null: false,
foreign_key: { to_table: :admin_products, primary_key: "ect" }
再次运行迁移。这应该可以解决它。
此外,最好设置 PostgreSQL 进行开发。尝试运行该迁移时会引发错误,但 SQLite 似乎可以进行迁移,但后来会抱怨。
https://api.rubyonrails.org/classes/ActiveRecord/ModelSchema/ClassMethods.html#method-i-table_name
评论
Admin::Product.find_by
CartProduct
t.references :product
评论
rails db:migrate
belongs_to :product, :class_name => "Admin::Product"
belongs_to :product, :class_name => "AdminProduct"
admin_products
AdminProduct model