Mel Mel - 1 year ago 75
Ruby Question

superclass mismatch for class User - inheriting from ActiveRecord::Base

I am trying to figure out my superclass mismatch error. All the posts I've read about this describe the problem as being that User is defined twice as a class in my application.

In my case, it isn't defined twice. I have a services folder and within that I have a user folder (for user service classes). In that user folder, I have a file called organisation_mapper_service.rb, with:

class User < ActiveRecord::Base
class OrganisationMapperService
def u)
new(user: user).call

def initialize(user: u)
self.user = user

def call
if matching_organisation.present?
# user.update_attributes!(organisation_id:
#SystemMailer.unmatched_organisation(user: user).deliver_now


attr_accessor :user

def matching_organisation

Separate to that, I have my user model which defines user as:

class User < ApplicationRecord

I thought it should be fine to define the service class in the way I have because it inherits from ActiveRecord::Base rather than ApplicationRecord.

Can anyone see what I've done wrong here? Where else could I look for a second definition of User?


I change the user organisation mapper service to open as follows:

class User::OrganisationMapperService < ActiveRecord::Base

But that then gives an error with my Users::OrgRequestsController which has new defined as follows:

def new
@all_organisations =, :id).map { |org| [org.title,] }
@org_request =

matched_organisation =
@org_request.organisation_id = matched_organisation.try(:id)

the error message then says:

PG::UndefinedTable at /users/4/org_requests/new
ERROR: relation "user_organisation_mapper_services" does not exist
LINE 8: WHERE a.attrelid = '"user_organisation_mapper...


I change my service class to:

class User::OrganisationMapperService

But then I get an error that says:

wrong number of arguments (given 1, expected 0)

That error highlights this line of my service class:

def initialize(user: u)
self.user = user

I don't know what to do about that because I clearly have a user if there is an inheritance from user.

Answer Source

Even once you solve all your other issues, you actually have an infinite recursion going on. User.first)

Is equivalent to calling: User.first).call

Which internally calls matching_organisation, so is sort of equivalent to: User.first).matching_organisation

Meanwhile, matching_organisation calls

It's just going to go round and round in circles.

The only reason it doesn't is because of the wrong number of arguments (given 1, expected 0) error. This is because it should be user) rather than in your matching_organisation method.

Update in response to comment:

From what I understand, the User::OrganisationMapperService is a service class that does the job of finding some Organisation and then performing some sort of work.

The User::OrganisationMapperService#matching_organisation method should actually contain the code that returns the matching organisation for the given user. The implementation will completely depend on how you have structured your database, but I'll give a couple of examples to put you on the right track or give you ideas.

First, Your organisations table may have a user_id column. In this case you could do a simple query on the Organisation model and perform a search using the user's id:

class User::OrganisationMapperService
  def matching_organisation
    # find the organisation and cache the result
    @matching_organisation ||= ::Organisation.where(user_id: user).first

Alternatively, you may have some sort of join table where there may be multiple Users at an Organisation (just for this example let us call this table 'employments'):

class Employment < ApplicationRecord
  belongs_to :user
  belongs_to :organisation

We can add scopes (this is a must read) to the Organisation model to assist with the query:

class Organisation < ApplicationRecord

  has_many :employments
  has_many :users, through: :employments 

  scope :for_user, ->(user) {
    # return organisations belonging to this user
    joins(:users).merge( Employment.where(user_id: user) )


Then finally, the OrganisationMapperService#matching_organisation method becomes:

class User::OrganisationMapperService
  def matching_organisation
    # find the organisation and cache the result
    @matching_organisation ||= ::Organisation.for_user(user).first
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download