0

I have a rails app with the model "quotes" and the model "orders". Orders take data from Quotes upon creation. Both the "quotes" and "orders" have a has_many attachments relationship. How can I copy over all the attachments from the "quotes" to the "orders". They do not have to recreate the file, just pointing to the same thing.

Suavocado
  • 949
  • 12
  • 27
  • Is there anything that you've tried? Could you post some code? – sjagr Aug 27 '15 at 18:59
  • @sjagr I tried to implement this method http://stackoverflow.com/questions/2739839/how-to-copy-a-file-using-paperclip but it doesn't say anything about a has_many relationship. – Suavocado Aug 27 '15 at 19:23
  • 1
    And you didn't try `quotes.attachments = orders.attachments` or vice-versa? – sjagr Aug 27 '15 at 19:24
  • I didn't think about that, I'll try right now and report back. (still new to rails) – Suavocado Aug 27 '15 at 19:26
  • 1
    Don't forget to use `save` after! – sjagr Aug 27 '15 at 19:28
  • @sjagr Okay, that worked. I used order.attachments = quote.attachments in the controller where quote was set to a specific quote already. Have all my helpful flags! – Suavocado Aug 27 '15 at 19:42
  • 1
    Glad to help. Remember that a lot of stuff in Ruby/RoR may be way simpler than you could ever think! – sjagr Aug 27 '15 at 19:57

1 Answers1

1

Akin to this question, we found that you can just as easily do:

order.attachments = quote.attachments 
order.save 

That's it!

Community
  • 1
  • 1
sjagr
  • 15,983
  • 5
  • 40
  • 67