0

I've been slaving at this problem for a day an a half now. I'm using the Mailboxer gem to to create a very cut-and-dried messaging system; one that's identical to the sample - https://github.com/RKushnir/mailboxer-app - just to start off with this gem.

I literally have both repositories sitting next to each other on my computer. The mailboxer-specific models, migrations, initializers, everything I can find. This includes the User model, with the name action. I've even tried messing around with the Notification model with the attr_accessible.

The databases are identical.

When I try in the console/seeds to create a Message after a conversation is saved in the sample app, it's quite easy. Once the conversation is saved I run:

n = Message.new
n.sender = User.find(1)
n.subject = 'Hi'
n.body = 'Hello there.'
n.conversation_id = Conversation.find(1).id
n.save

And boom. It works beautifully. My app, on the other hand, will function identically; until it gets to the sender field.

When I try to set the sender:

n.sender = User.find(1)

I receive this error: NoMethodError: undefined method 'sender=' for #<Message:0x00000101708eb8>

Why??

This is driving me crazy. I would really be appreciative if someone could lend some assistance. Thank you in advance.

Message/Notification schema :

  create_table "notifications", force: true do |t|
    t.string   "type"
    t.text     "body"
    t.string   "subject",              default: ""
    t.integer  "sender_id",                            null: false
    t.integer  "conversation_id"
    t.boolean  "draft",                default: false
    t.datetime "updated_at",                           null: false
    t.datetime "created_at",                           null: false
    t.integer  "notified_object_id"
    t.string   "notified_object_type"
    t.string   "notification_code"
    t.string   "attachment"
    t.boolean  "global",               default: false
    t.datetime "expires"
  end

  add_index "notifications", ["conversation_id"], name: "index_notifications_on_conversation_id", using: :btree

This is what the gem generates, and it is identical to the sample app as well.

user3181113
  • 758
  • 2
  • 11
  • 23
  • Can you put you message schema code. It say there is no sender field for your message. – sonnyhe2002 Feb 24 '14 at 05:49
  • What bothers me is that- sure, theres no sender field - but because the migration is `t.references :sender`, it creates the `sender_id` and `sender_typ`e field, which I am able to set through the sender field in the console for the sample app. but not my app – user3181113 Feb 24 '14 at 05:54
  • And, on top of that, I've tried deleting the sender field and replacing it with a simple sender id, then raking my migrations, but I still get the same error – user3181113 Feb 24 '14 at 05:56
  • So the sender is another table, thats why there is a foreign_key called sender_id, Does you code have a sender model – sonnyhe2002 Feb 24 '14 at 05:58
  • No, but neither does the sample app. I guess the problem lies somewhere in the function of `t.references` for the :sender. Is there something I'm not aware of about `t.references`? – user3181113 Feb 24 '14 at 06:00

1 Answers1

0

Take a look at Mailboxer gem sent_message method here you might find it helpful.

You can also use the mailboxer built in send_message method to send message

sender = User.find(1)
receiver = User.find(2) 
sender.send_message(receiver,"Subject", "Body")
Monideep
  • 2,790
  • 18
  • 19
  • I get that, but I can't even set the sender. It's not even when I try to save. It's just when I try to set the sender field (n.sender = User.find(1) will raise the mentioned error). I can't even successfully achieve the first step on that list. And, I'm doing this through the console, not the controller. – user3181113 Feb 24 '14 at 06:13
  • You are getting this error because Message model does not have a sender attribute. – Monideep Feb 24 '14 at 06:16
  • There is no message model on either my app or the sample one though, yet the Message.create and Message.new works on the sample app. – user3181113 Feb 24 '14 at 06:17
  • First make sure you have added act_as_messagable in your User model and then in console instead of doing something like n.sender simply find two user for eg say user1 and user2 and then use the send_message method to send message. This method will set all association for you. – Monideep Feb 24 '14 at 06:25
  • I've done the act_as_messagable. I'll try the those methods. I'll tell you how it goes in a minute. – user3181113 Feb 24 '14 at 06:28
  • Perhaps I'm way off - but following the exact syntax of the send_message action, this is what I did `q = u.send_message(recipients: z, msg_body: 'hello', subject: 'hi',sanitize_text: true, attachment: nil, message_timestamp: Time.now)` with `u = User.find(1)` and `z = User.find(8)` – user3181113 Feb 24 '14 at 06:38
  • try this u = User.find(1) z = User.find(8) conversation = u.send_messae(z, "Subject of the message", "Body of the message") – Monideep Feb 24 '14 at 06:44
  • Then try u.mailbox.inbox.first to get the first conversion – Monideep Feb 24 '14 at 06:47
  • Error: `[14] pry(main)> conversation = u.send_message(z, "hello", "hi") WARNING: Can't mass-assign protected attributes for Conversation: subject WARNING: Can't mass-assign protected attributes for Message: body, subject ActiveRecord::UnknownAttributeError: unknown attribute: sender_type` – user3181113 Feb 24 '14 at 06:47
  • Is there a way to disable these gem-default protected attributes? – user3181113 Feb 24 '14 at 06:48
  • Are you using Rails 4? And also make sure you don't have Message and Conversation model in your app. – Monideep Feb 24 '14 at 06:56
  • I'm using rails 4. I had a Conversation model, but I deleted it, reloaded the database, ran the same commands, still got the same error. – user3181113 Feb 24 '14 at 07:02
  • Try to use the latest version of mailbox. In your gemfile use gem 'mailboxer' without any version number and then bundle install if you still have problem let me know I will share my mailboxer implementation with you. – Monideep Feb 24 '14 at 07:08
  • Deleted the version (which was 0.11.0), then deleted the Gemfile.lock just to make sure, then bundled. I'm back to 0.11.0 and I tried the console commands again, and I'm still getting the same error. – user3181113 Feb 24 '14 at 07:12
  • Figured it out. Just played hardcore search and destroy with every piece of code installed from Mailboxer. Re-installed Mailboxer, made sure everything was one hundred percent perfect. And it worked. Just like that. Don't know what went wrong in the first place, but my console seemed to be trying to match the User with a field called sender, rather than the fields that sender created. Weird. – user3181113 Feb 24 '14 at 09:36