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

有些话、适合烂在心里 提交于 2019-12-01 16:26:48

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!