Search code examples
rubyruby-on-rails-5activeadminself-reference

unable to save data active admin slef association rails 5.2


DB structure

id | name | category_id(parent_id) |

when I try to save the data using active admin it gives me below error message, please find below details along with model.

processing by Admin::CategoriesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"Wj+Au5xC6mfESE/OXhLNM8a33Sy9Arr0pXvDnpAZdJxo/wfOLXhklVnkUbfdvEaUN0deAh78jqUqDIyCLCjwKQ==", "category"=>{"name"=>"tech", "category_id"=>"2"}, "commit"=>"Create Category"}
  User Load (1.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ /home/adi/.rvm/gems/ruby-2.5.7/gems/activerecord-5.2.4.1/lib/active_record/log_subscriber.rb:98
   (0.4ms)  BEGIN
  ↳ /home/adi/.rvm/gems/ruby-2.5.7/gems/activerecord-5.2.4.1/lib/active_record/log_subscriber.rb:98
   (0.4ms)  ROLLBACK
  ↳ /home/adi/.rvm/gems/ruby-2.5.7/gems/activerecord-5.2.4.1/lib/active_record/log_subscriber.rb:98
  Rendering /home/adi/.rvm/gems/ruby-2.5.7/gems/activeadmin-1.4.3/app/views/active_admin/resource/new.html.arb
  Category Load (0.8ms)  SELECT "categories".* FROM "categories"
  ↳ app/admin/categories.rb:16
  Rendered /home/adi/.rvm/gems/ruby-2.5.7/gems/activeadmin-1.4.3/app/views/active_admin/resource/new.html.arb (640.2ms)
Completed 200 OK in 746ms (Views: 644.9ms | ActiveRecord: 15.8ms)


Started POST "/admin/categories" for 127.0.0.1 at 2020-03-10 23:14:52 +0530
Processing by Admin::CategoriesController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"WNo/hAr1v+g75CQeyTPTFH2J8u9OuOhPM4bLPPFGKnRqGrjxu88xGqZIOmdKnVizjHlxwe1G3B688YQgTXeuwQ==", "category"=>{"name"=>"tech", "category_id"=>"2"}, "commit"=>"Create Category"}
  User Load (0.8ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ /home/adi/.rvm/gems/ruby-2.5.7/gems/activerecord-5.2.4.1/lib/active_record/log_subscriber.rb:98
   (0.3ms)  BEGIN
  ↳ /home/adi/.rvm/gems/ruby-2.5.7/gems/activerecord-5.2.4.1/lib/active_record/log_subscriber.rb:98
   (0.3ms)  ROLLBACK
  ↳ /home/adi/.rvm/gems/ruby-2.5.7/gems/activerecord-5.2.4.1/lib/active_record/log_subscriber.rb:98
  Rendering /home/adi/.rvm/gems/ruby-2.5.7/gems/activeadmin-1.4.3/app/views/active_admin/resource/new.html.arb
  Category Load (0.7ms)  SELECT "categories".* FROM "categories"
  ↳ app/admin/categories.rb:16
  Rendered /home/adi/.rvm/gems/ruby-2.5.7/gems/activeadmin-1.4.3/app/views/active_admin/resource/new.html.arb (147.3ms)
Completed 200 OK in 179ms (Views: 152.4ms | ActiveRecord: 2.6ms)

Model:-

class Category < ApplicationRecord
    has_many :blogs
    belongs_to :category, :class_name => 'Category'
    has_many :category, :class_name => 'Category', :foreign_key => 'category_id'
    # belongs_to :Category, :class_name => 'Category'
    scope :category_name, -> (id) { find(id).name }
end

active admin

ActiveAdmin.register Category do
    permit_params :name, :category_id

    index do
      selectable_column
      id_column
      column :name
      column :category_id
      actions
    end


    form do |f|
      f.inputs do
        f.input :name
        f.input :category_id, :label => 'Category',:as => :select, :class => 'form-control',:collection => Category.all.map{|category| ["#{category.name}", category.id]}
      end
      f.actions
    end

end

also I want it is to be select like this, if i have 2 parent category music and video with id 1 and 2 and 2 child rock and silent music with category id 1 then in form if i select parent category music then child category dropdown automatically contain rock and silent music value.


Solution

  • There is probably some validation error that is occuring. If you define the associations like below you will have a better system -

      belongs_to :parent_category, foreign_key: 'category_id', class_name: 'Category', optional: true
      has_many :sub_categories, class_name: 'Category', foreign_key: 'category_id',
               dependent: :destroy
    

    Then normally you can access sub_categories and a parent_category easily. And hopefully active_admin errors will be gone. Comment if there's any more error in active_admin.