1

How to successfully inherit ActiveRecord::Base?

Environment: Ruby 2.0.0, Rails 4.0.3, Windows 8.1, PostreSQL 9.3.3, Devise 3.2.4

I have an operational app and would like to add a comprehensive logging class to it. This will be a complex class that not only logs messages but also creates an SQL database that logs transactions by object. I need this class available throughout all of the classes in the application.

To do this, I wanted to inherit ActiveRecord::Base into the class and then have all other classes inherit it, though I don't plan to use STI. That seemed to be a lot simpler in concept than in practice, even though I thought such inheritance was a common best practice. Am I missing something?

One of the initial tables was this:

class Device < ActiveRecord::Base
...
end

I set it up like this:

class XLog < ActiveRecord::Base
  self.abstract_class = true
  def initialize
  end
end
class Device < XLog
...
end

Prior to this change, the app was working fine. After this change, when I login I receive:

ArgumentError at /devices/sign_in
wrong number of arguments (1 for 0)

The error occurs in:

bin/rails, line 4

bin/rails is:

#!/usr/bin/env ruby.exe
APP_PATH = File.expand_path('../../config/application',  __FILE__)
require_relative '../config/boot'
require 'rails/commands'

Device is the Devise "User" class in this application and the error occurs when I try to login. If I change Device to inherit ActiveRecord::Base, it lets me login and run.

But, then I get another error whenever I call "new" on the other classes:

undefined method `[]' for nil:NilClass

I am definitely missing something when it comes to this inheritance. Advice appreciated.

1 Answer 1

1

The initialize method was throwing the error. It was triggering every time a subclass was initialized and was configured to accept 0 parameters. When I removed it, the whole thing started working. If I need it, I'll have to configure it to accept a variable number of parameters and pass them as expected, I guess.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.