-1

I did everything pretty much as described here: question

But I keep getting error:

NoMethodError: undefined method `parent_model' for Stream (call 'Stream.connection' to establish a connection):Class

In model/concerns faculty_block.rb

module FacultyBlock
  extend ActiveSupport::Concern
  included do
    def find_faculty
      resource = self
      until resource.respond_to?(:faculty)
        resource = resource.parent
      end
   resource.faculty
    end
    def parent
      self.send(self.class.parent)
    end
  end

  module ClassMethods
    def parent_model(model)
      @@parent = model
    end
  end

end

[Program, Stream, Course, Department, Teacher].each do |model|
  model.send(:include, FacultyBlock)
  model.send(:extend, FacultyBlock::ClassMethods) # I added this just to try
end

In initializers:

require "faculty_block"

method call:

class Stream < ActiveRecord::Base
 parent_model :program
end
Community
  • 1
  • 1
Joe Half Face
  • 2,051
  • 13
  • 38

1 Answers1

0

It seems that the Stream is loaded before loading concern, make sure that you have applied the concerns inside the class definition. When rails loader matches class name for Stream constant, it autoloads it before the finishing evaliation of the faculty_block, so replace constants in it with symbols:

[:Program, :Stream, :Course, :Department, :Teacher].each do |sym|
   model = sym.to_s.constantize
   model.send(:include, FacultyBlock)
   model.send(:extend, FacultyBlock::ClassMethods) # I added this just to try
end
Малъ Скрылевъ
  • 14,754
  • 4
  • 47
  • 62