Rails 4: undefined method `relation_delegate_class' for Model:Class

The problem was tricky to identify, especially just from the content of the question.

THE PROBLEM

The reason why I was getting the undefined method 'relation_delegate_class' for Invite:Class error is because Invite was no longer considered to be a model by Rails.

THE ROOT CAUSE OF THE PROBLEM

When I created the Invite mailer, I ran rails g mailer Invite instead of rails g mailer InviteMailer.

Because of this, Invite as a mailer override Invite as a model, hence creating errors as soon as methods were applied to instances of the Invite model.

HOW WE FIGURED IT OUT

One of my friends, who is way more experienced with programming than I am, identified the problem by tweaking the @invite = @calendar.invites.build line that was causing the error.

This led us to eventually run Invite.first in the rails console: while we should have got either an instance of the Invite class, or nil, we actually got an error.

Since .first should be a valid method on any ActiveRecord model, we realized that Invite was not a considered to be a model by Rails.

HOW WE FIXED IT

Once we had identified the issue, fixing it was pretty straightforward:

  • We changed the name of the Invite mailer from invite.rb to invite_mailer.rb
  • In the newly renamed invite_mailer.rb file, we replaced class Invite < ApplicationMailer with class InviteMailer < ApplicationMailer

I hope this can be useful to other Stack Overflow users who might get a similar relation_delegate_class error.