LoadError Unable to autoload constant Message

Ruby on-RailsActivemodelRails ModelsMailboxer

Ruby on-Rails Problem Overview


In my app; when I submit form, I get this error:

LoadError at /questions
Unable to autoload constant Message, expected /app/models/message.rb to define it

It points to the create action in the Questions controller:

@message = current_user.messages.new(:subject => "You have a question from #{@question.sender_id}"`

Message model:

class Mailboxer::Message < ActiveRecord::Base
  attr_accessible :notification_id, :receiver_id, :conversation_id
end

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

By convention in rails (and this is enforced by autoloader), file paths should match namespaces.

So, if you have a Mailboxer::Message model, it should be in app/models/mailboxer/message.rb.

Additionally, you probably have autoloader kicking in when trying to load a Message class (my guess is that it happens from within ActAsMessageable). It looks for a message.rb file in load path, find it in app/model/ and thus load that file so it can find the Message class.

Problem is, it doesn't find a Message class in that file, only a Mailboxer::Message class (which is radically different). This is why it throws "Unable to autoload constant Message, expected /app/models/message.rb to define it".

To fix that, create directory app/models/mailboxer/ and put Mailboxer::Message in it.

Solution 2 - Ruby on-Rails

I got this during integration testing. Turns out, it was fixtures related. Had to delete the my unused file in /test/fixtures/wrong_name.yml

Solution 3 - Ruby on-Rails

As stated in the documentation, to send a message from A model to B model you have to add:

acts_as_messageable 

in both models.

And then do:

a.send_message(b, "Body", "subject")

So in your models:

  class User < ...
    act_as_messageable
  end

@question_sender must be a User instance.

@question_sender.send_message({attr_accessor_hash}, recipient_user, @question.body, "You have a question from #{@question_sender.id}")

As long as the attr_accessor is not related to the gem, and the method send_message is not aware of this attributes you will have to redefine it:

https://github.com/mailboxer/mailboxer/blob/master/lib/mailboxer/models/messageable.rb#L60

add the attr_accessor_hash to method

def send_message({attr_accesor_hash}, recipients, msg_body, subject, sanitize_text=true, attachment=nil, message_timestamp = Time.now)

And look at the code add the fields where you need as: attr_accessor["param"]

Solution 4 - Ruby on-Rails

Notice these lines;

@question = Question.new(params[:question])

@question.message = @message

and ;

attr_accessible :answer, :question, :sender_id, :recipient_id, :conversation_id

The @question.message line is calling an attribute that is not accessible in the Question Model so do this;

attr_accessible :answer, :question, :sender_id, :recipient_id, :conversation_id, message

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionpwz2000View Question on Stackoverflow
Solution 1 - Ruby on-RailskikView Answer on Stackoverflow
Solution 2 - Ruby on-RailsJayView Answer on Stackoverflow
Solution 3 - Ruby on-RailsJorge de los SantosView Answer on Stackoverflow
Solution 4 - Ruby on-RailsAcaciaView Answer on Stackoverflow