gpt4 book ai didi

ruby-on-rails - 具有 join_table 和 has_many 的模型的路由错误 :through in RailsAdmin

转载 作者:行者123 更新时间:2023-12-04 20:14:37 25 4
gpt4 key购买 nike

所以我有 3 个模型:category , product , category_products .

这是我的 category.rb

  attr_accessible :name
has_many :category_products do
def with_products
includes(:product)
end
end

has_many :products, :through => :category_products

这是我的 product.rb
  attr_accessible :name, :description, :price, :vendor_id, :image, :category_ids

belongs_to :vendor
has_many :category_products do
def with_categories
includes(:category)
end
end

has_many :categories, :through => :category_products

这是我的 category_product.rb
  attr_accessible :product_id, :category_id, :purchases_count

belongs_to :product
belongs_to :category

validates_uniqueness_of :product_id, :scope => :category_id

这是我的 routes.rb
  mount RailsAdmin::Engine => '/admin', :as => 'rails_admin'
resources :categories
resources :vendors do
resources :products
end

authenticated :user do
root :to => 'home#index'
end

root :to => "home#index"
devise_for :users
resources :users

当我点击 Categories当我查看 RailsAdmin 时,出现此错误:
ActionController::RoutingError at /admin/category

Message No route matches {:action=>"show", :model_name=>"category_product", :id=>nil, :controller=>"rails_admin/main"}

当我单击 Category Products 时,我也收到此错误消息
ActiveRecord::StatementInvalid at /admin/category_product

Message SQLite3::SQLException: no such column: category_products.desc: SELECT "category_products".* FROM "category_products" ORDER BY category_products. desc LIMIT 20 OFFSET 0

RailsAdmin 中用于我的其他“正常”(即非 HMT)模型的所有其他链接都有效。

什么可能导致这种情况?

谢谢。

编辑 1

值得一提的是,以下是我在 Rails Admin 中单击“类别”时的日志:
CodeRay::Scanners could not load plugin nil; falling back to :text
CodeRay::Scanners could not load plugin nil; falling back to :text


Started GET "/admin/category?_pjax=%5Bdata-pjax-container%5D" for 127.0.0.1 at 2012-12-20 22:23:38 -0500
Processing by RailsAdmin::MainController#index as HTML
Parameters: {"_pjax"=>"[data-pjax-container]", "model_name"=>"category"}
Category Load (0.3ms) SELECT "categories".* FROM "categories" LIMIT 6
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Category Load (0.2ms) SELECT "categories".* FROM "categories" ORDER BY categories.id desc LIMIT 20 OFFSET 0
CategoryProduct Load (0.2ms) SELECT "category_products".* FROM "category_products" WHERE "category_products"."category_id" = 2
Rendered /.rvm/gems/ruby-1.9.3-p194@apt-605/gems/rails_admin-0.3.0/app/views/rails_admin/main/index.html.haml within layouts/rails_admin/pjax (29.4ms)
Completed 500 Internal Server Error in 43ms
CodeRay::Scanners could not load plugin nil; falling back to :text
CodeRay::Scanners could not load plugin nil; falling back to :text


Started GET "/admin/category" for 127.0.0.1 at 2012-12-20 22:23:40 -0500
Processing by RailsAdmin::MainController#index as HTML
Parameters: {"model_name"=>"category"}
Category Load (0.3ms) SELECT "categories".* FROM "categories" LIMIT 6
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Category Load (0.2ms) SELECT "categories".* FROM "categories" ORDER BY categories.id desc LIMIT 20 OFFSET 0
CategoryProduct Load (0.2ms) SELECT "category_products".* FROM "category_products" WHERE "category_products"."category_id" = 2
Rendered /.rvm/gems/ruby-1.9.3-p194@apt-605/gems/rails_admin-0.3.0/app/views/rails_admin/main/index.html.haml within layouts/rails_admin/application (30.5ms)
Completed 500 Internal Server Error in 251ms

编辑 2

这是一个 gist of the full trace的错误。我正在使用 gem better_errors,所以跟踪看起来不像标准的 Rails 跟踪错误。但是数据是一样的。

编辑 3

这是我的 3 个模型的架构:
CategoryProducts
# == Schema Information
#
# Table name: category_products
#
# product_id :integer
# category_id :integer
# purchases_count :integer default(0)
# created_at :datetime not null
# updated_at :datetime not null
Category
# == Schema Information
#
# Table name: categories
#
# id :integer not null, primary key
# name :string(255)
# created_at :datetime not null
# updated_at :datetime not null
Product
# == Schema Information
#
# Table name: products
#
# id :integer not null, primary key
# name :string(255)
# description :string(255)
# price :float
# vendor_id :integer
# created_at :datetime not null
# updated_at :datetime not null
# image :string(255)

请注意 CategoryProduct没有主键字段。是这个问题吗?

最佳答案

检查所有 CategoryProduct 对象是否有外键:category_id 和 product_id。

CategoryProduct.all.each {|c| raise("Repair #{c.id}") unless c.category && c.product}

12 月 21 日更新:

我如何使用 Category、Product 和 CategoryProduct 安装全新的 Rails 3.2.9。模型的关系代码是相同的,RailsAdmin 使用默认设置。

它没有任何问题!

我决定检验我的假设。我想也许当您从 HABTM 转到 HM2HM 时,您已经错过(忘记)重新建立 CategoryProduct 的键列 ID,它现在不仅仅是一个连接模型,而是一个独立的实体。

因此,路由错误如下:
No route matches {:action=>"show", :model_name=>"category_product", :id=>nil, :controller=>"rails_admin/main"}

可能是缺少 id 的结果。

好吧,我手动禁用了 CategoryProduct 的 id 字段(def id;nil;end)。

是的,它是:
No route matches {:action=>"show", :model_name=>"category_product", :id=>nil, :controller=>"rails_admin/main"}

关于ruby-on-rails - 具有 join_table 和 has_many 的模型的路由错误 :through in RailsAdmin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13966643/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com