Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Routing Error for a model with a join_table and has_many :through in RailsAdmin

So I have 3 models: category, product, category_products.

This is my category.rb

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

  has_many :products, :through => :category_products

This is my 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

This is my 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

This is my 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

When I click on Categories when I am viewing RailsAdmin, I get this error:

ActionController::RoutingError at /admin/category

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

I also get this error when I click on 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

All other links within RailsAdmin for my other 'normal' (i.e. non HMT) models work.

What could be causing this?

Thanks.

Edit 1

For what it's worth, here are the logs when I click on 'Categories' inside of 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

Edit 2

Here is a gist of the full trace of the error. I am using the gem better_errors, so the trace doesn't look like a standard Rails trace error. But the data is the same.

Edit 3

This is the schema for my 3 models:

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)

Notice that CategoryProduct doesn't have a primary key field. Is that the issue?

like image 670
marcamillion Avatar asked Oct 06 '22 14:10

marcamillion


1 Answers

Check all the CategoryProduct objects have foreign keys: category_id and product_id.

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

Dec 21 update:

How i've installed brand new Rails 3.2.9 with Category, Product and CategoryProduct. Model's relationship code is identical, RailsAdmin with default settings.

And it works with no any problems!

I decided to test my hypothesis. I think maybe when you went from the HABTM to the HM2HM you have missed (forgot) to reestablish key column ID for CategoryProduct which is now not just a join-model, but an independent entity.

So, routing error like:

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

can be result of missing id.

Well I disabled CategoryProduct's id field manually (def id; nil; end).

and yes it is:

No route matches {:action=>"show", :model_name=>"category_product", :id=>nil, :controller=>"rails_admin/main"}
like image 138
Valery Kvon Avatar answered Oct 10 '22 02:10

Valery Kvon