ruby-on-railsself-referencing-table

Ruby on Rails Self Referential Create Action Returning Unknown Attribute Error


I am creating a self referential relationship for services. The idea is to allow you to add a potentially infinite level of sub services to a service, so services can have children, and those children can have children, etc.

To do this I have crated two classes, Services, and SubServices. SubServices is a simple join table with a parent_service_id and a child_service_id. I am able to create everything in the rails console and it works just fine. Both the child and parent associations work. It's just in the controller that it's breaking down

schema:

  create_table "services", force: :cascade do |t|
    t.string   "name"
    t.integer  "business_category_id"
    t.datetime "created_at",           null: false
    t.datetime "updated_at",           null: false
    t.index ["business_category_id"], name: "index_services_on_business_category_id", using: :btree
  end

  create_table "sub_services", force: :cascade do |t|
    t.integer  "child_service_id"
    t.integer  "parent_service_id"
    t.datetime "created_at",        null: false
    t.datetime "updated_at",        null: false
    t.index ["child_service_id"], name: "index_sub_services_on_child_service_id", using: :btree
    t.index ["parent_service_id"], name: "index_sub_services_on_parent_service_id", using: :btree
  end

models:

class SubService < ApplicationRecord
  belongs_to :parent_service, class_name: 'Service'
  belongs_to :child_service, class_name: 'Service'
end

class Service < ApplicationRecord
  belongs_to :business_category

  has_many :client_services     # TODO REMOVE
  has_many :clients, :through => :client_services   # TODO REMOVE

    has_many :business_services
  has_many :businesses, :through => :business_services

  has_many :parental_services, foreign_key: :parent_service_id, class_name: "SubService"
  has_many :child_services, through: :parental_services

  has_many :children_services, foreign_key: :child_service_id, class_name: "SubService"
  has_many :parent_services, through: :children_services

  validates :name, presence: true
  validates :name, uniqueness: { scope: :business_category, message: "service already added" }

end

SubService new action:

  def new
    @sub_service = @service.child_services.new
    respond_to do |format|
      format.html
      format.js { render :new }
    end
  end

SubService create action:

  def create
    @sub_service = @service.child_services.new(service_params);
    # binding.pry
    if @sub_service.save
      redirect_to(admin_business_categories_path)
    end
  end

service_params:

    def service_params
      params.require(:sub_service).permit(:name)
    end

Sub Service new view:

  <%= render 'form', f: f %>
<% end %>

_form:

<div class="col-12  col-md-10">
  <div class="col-12  col-md-12">
    <%= f.input :name, label: 'Service Name' %>
  </div>
</div>

<div class="col-12  col-md-2">
  <div class="btn-group-vertical" role="group" aria-label="...">
    <button id="serviceFormCancelButton" class="btn btn-danger">CANCEL</button>
    <%= f.submit 'SAVE', class: 'btn btn-success' %>
    <br>
  </div>
</div>

This is the error my console is returning

ActiveModel::UnknownAttributeError (unknown attribute 'parent_service_id' for Service.):

app/controllers/admin/sub_services_controller.rb:14:in `create'
  Rendering C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
  Rendering C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
  Rendered C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (3.0ms)
  Rendering C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
  Rendered C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.0ms)
  Rendering C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
  Rendered C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.0ms)
  Rendered C:/Ruby24-x64/lib/ruby/gems/2.4.0/gems/actionpack-5.0.7/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (846.1ms)

Solution

  • You want a Service > SubService and then potentially Service > SubService > SubService > SubService.... right? You're getting the unkown attribute error because parent_id is on SubService. You can do what you want with just the Service model. Just put the parent_id on that.

    You could then get rid of the SubService model.

    class Service
    
     belongs_to :parent_service, foreign_key: :parent_id, class_name: 'Service'
     has_many :child_services, class_name: 'Service'
    
    end