Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LoadError Unable to autoload constant Message

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
like image 832
pwz2000 Avatar asked Jun 20 '14 16:06

pwz2000


3 Answers

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.

like image 86
kik Avatar answered Nov 03 '22 04:11

kik


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

like image 29
Jay Avatar answered Nov 03 '22 05:11

Jay


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"]

like image 1
Jorge de los Santos Avatar answered Nov 03 '22 05:11

Jorge de los Santos