I’m trying to split a large model into multiple files for logical organization. So i have two files:
model1.rb
class Model1 < ActiveRecord::Base
before_destroy :destroying
has_many :things, :dependent=>:destroy
def method1
...
end
def method2
...
end
end
require 'model1_section1'
model1_section1.rb
class Model1
def method3
...
end
def self.class_method4
...
end
end
but when the app loads, and there is a call to Model1.class_method4, i get:
undefined method `class_method4' for #<Class:0x92534d0>
i’ve also tried this for the require:
require File.join(File.dirname(__FILE__), 'model1_section1')
What am i doing wrong here?
I know I’m answering this a little late, but I’ve just done this in one of my apps so thought I’d post the solution I used.
Let’s this was my model:
You can refactor it to:
and:
It works perfectly thanks to the extra cleanliness that
ActiveSupport::Concerngives you. Hope this solves this old question.