だから私は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 内の [Categories] をクリックしたときのログを次に示します。
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
エラーの完全なトレースの要点を次に示します。私は 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
主キー フィールドがないことに注意してください。それが問題ですか?