Search code examples
ruby-on-railspolymorphic-associationsnameerroruninitialized-constant

Uninitialized constant Model:: Error with polymorphic relation


I created a polymorphic relation in a book reviewing app that I am writing. My app has the nested models: Piece >> Section >> Subsection >> Subsubsection, and I have created a model which belongs to all of these called KeyConcept, my intention being that each of these can have a key concept. When trying to display the "Show" view of the "Piece" model i get the following error:

NameError in Pieces#show
uninitialized constant Piece::Keyconcept

<h5>Summary: </h5><p><%= simple_format(@piece.summary) %></p>
<br/>
<% @piece.keyconcepts.each do |concept| %>
    <li>
      <%= link_to concept.definition, r, class: 'section_name' %>
    </li>

So the routes.rb file looks like this:

resources :pieces do
  resources :sections do
    resources :subsections do 
      resources :subsubsections
    end
  end
  resources :links
end

resources :pieces, :sections, :subsections, :subsubsections do
  resources :connections, only: [:index, :new, :edit, :update, :destroy, :create]
  resources :keyconcepts, only: [:index, :new, :edit, :update, :destroy, :create, :show]
end

the model.rb files look like this:

in models/concerns/conceptable.rb

module Conceptable
  extend ActiveSupport::Concern

  included do
    has_many :keyconcepts, as: :conceptable
  end
end

key_concept.rb

class KeyConcept < ActiveRecord::Base
    belongs_to :conceptable, polymorphic: true
end

piece.rb

class Piece < ActiveRecord::Base
    include Connectable
    include Conceptable
    has_many :sections
    has_many :subsections, through: :sections
    has_many :links
end

I don't know if this is a problem in the controller?

class KeyconceptsController < ApplicationController
    include KeyconceptsHelper

    def whereami
        if params[:piece_id]
            @piece = Piece.find(params[:piece_id])

            here = @piece
            parameter = :piece_id
            type = "Piece"
        elsif params[:section_id]
            @section = ::Section.find(params[:section_id])
            @piece = @section.piece_id

            here = @section
            parameter = :section_id
            type = "Section"
        elsif params[:subsection_id]
            @subsection = Subsection.find(params[:subsection_id])
            @section = @subsection.section_id
            @piece = Section.find(id=@section).piece_id

            here = @subsection
            parameter = :subsection_id
            type = "Subsection"
        elsif params[:subsubsection_id]
            @subsubsection = Subsubsection.find(params[:subsubsection_id])
            @subsection = @subsubsection.subsection_id
            @section = Subsection.find(id=@subsection).section_id
            @piece = Section.find(id=@section).piece_id

            here = @subsubsection 
            parameter = :subsubsection_id
            type = "Subsubsection"
        end
end

def redirect
    if type == "Piece"
        redirect_to piece_path(@piece)
    elsif type == "Section"
        redirect_to piece_section_path(@piece, @section)
    elsif type == "Subsection"
        redirect_to piece_section_subsection_path(@piece, @section, @subsection)
    elsif type == "Subsubsection"
        redirect_to piece_section_subsection_subsubsection_path(@piece, @section, @subsection, @subsubsection)
    end
end

def index
    whereami.call
end 

def show
    whereami.call
    r = redirect.call
end

def new
    @keyconcept = KeyConcept.new
    @keyconcept.conceptable_id = here.id
end

def create
    whereami.call

  @keyconcept = KeyConcept.new(keyconcept_params)

  @keyconcept.conceptable_id = params[parameter]
  @keyconcept.conceptable_type = type
  @keyconcept.save

  redirect.call
end

def destroy
    here.destroy
    redirect.call
    flash.notice = "#{type} '#{here.name}'  from '#{@piece.name}' deleted!"
end

def edit
    whereami.call
end

def update
    whereami.call

    here.update(keyconcept_params)
    flash.notice = "#{type} '#{here.name}' Updated!"
    redirect.call
end
end

I have reloaded the console and I get the same error. I have also tried to do a few things in the console and this: Piece.first.keyconcepts, does not work (i get the same NameError: uninitialized constant Piece::Keyconcept) however this: KeyConcept.first DOES work, i even though i get nil because i havent created any instances yet.

I notice that, in the error message it says Keyconcept and not camelcase KeyConcept. I think this is where the problem lies but I do not have enough experience to understand it.

I would appreciate help in solving this!


Solution

  • The problem here is you are not following proper conventions

    Change your model name to Keyconcept and file_name to keyconcept.rb

    keyconcept.rb

    class Keyconcept < ActiveRecord::Base
        belongs_to :conceptable, polymorphic: true
    end
    

    OR

    You will need to change keyconcepts to key_concepts in the following places:

    • routes

      resources :keyconcepts
      
    • controller name

      class KeyConceptsController < ApplicationController
        ...
      end
      
    • controller file name

      key_concepts_controller.rb
      
    • strong params

      def key_concept_params
        params.require(:key_concept).permit(:your, :params)
      end
      
    • associations

      has_many :key_concepts