4

I'm using mailboxer gem and I don't know how to use it with Paperclip (Message Class).

Using Paperclip with a User class is:

class User < ActiveRecord::Base
  has_attached_file :picture
end

How can I add has_attached_file to the Message class (there is no message.rb in models)?

Thank you.

Alex
  • 2,036
  • 4
  • 22
  • 31
  • the gem is meant to send messages inside a web app. If you parse the messages you could add html thus display pictures. – apneadiving Aug 30 '12 at 14:24
  • Thank you for your answer but message should have any kind of file as attachment (jpg, pdf, ...). So `Message` class should have `has_mattached_file :document`. But the problem is how to do that considering that there is no message.rb in models folder. – Alex Aug 30 '12 at 14:27
  • actually, there are attachments! but with carrierwave, see: https://github.com/ging/mailboxer/blob/master/app/models/message.rb#L13 – apneadiving Aug 30 '12 at 14:29
  • Note: the above link doesn't work, use instead: https://github.com/ging/mailboxer/blob/4b2681c1790b823f7b493fb00b41e9899bb90ebe/app/models/message.rb#L13 – Purplejacket Dec 09 '13 at 20:27
  • Or, more to the point, look in message.rb for the line that reads `mount_uploader :attachment, AttachmentUploader` – Purplejacket Dec 09 '13 at 20:29

1 Answers1

2

Two solutions:

  • use the built-in attachment made with carrierwave (source)

  • monkey patch the Message model:

in an initializer do:

Message.class_eval do

  #your paperclip code goes here

end

And don't forget migrations!

apneadiving
  • 114,565
  • 26
  • 219
  • 213
  • I added this to an initializer file: `Message.class_eval do attr_accessible :document has_attached_file :document end` and did the migrations but I get `undefined method 'document_file_name'` when I run `Message.first.document` – Alex Aug 30 '12 at 14:52
  • here is my schema.rb messages table content after migrations: `create_table "messages", :force => true do |t| ..... ..... t.string "document_file_name" t.string "document_content_type" t.integer "document_file_size" t.datetime "document_updated_at" end` so the `document_file_name` is present in messages table. – Alex Aug 30 '12 at 14:56
  • ok it's sheer logic: `Message` is a STI, the root model is `Notification`. You should do like here: https://github.com/ging/mailboxer/blob/master/db/migrate/20111204163911_add_attachments.rb – apneadiving Aug 30 '12 at 15:00
  • You're right, it works. I run the migration for notifications table and did Notification.class_eval instead of Message.class_eval and it's working now. Can you add an answer so that I accept it. – Alex Aug 30 '12 at 15:12
  • no need to class_eval notification, the current answer is fine – apneadiving Aug 30 '12 at 15:13
  • @apneadiving, can you update your answer, the "source" link you give is wrong. I made a comment on the question (saying "the above link doesn't work ...") with corrected info. – Purplejacket Dec 09 '13 at 20:35
  • Also, that "migrations" link fails. This will work: https://github.com/ging/mailboxer/blob/4b2681c1790b823f7b493fb00b41e9899bb90ebe/db/migrate/20111204163911_add_attachments.rb – Purplejacket Dec 09 '13 at 20:44
  • Or ... well, in the newer version all migrations have been squished into one. Here is the current migrations file as of Dec 2013: https://github.com/ging/mailboxer/blob/master/db/migrate/20110511145103_create_mailboxer.rb – Purplejacket Dec 09 '13 at 20:46