<p>I'm currently using the awesome attachment-fu plugin for a Rails app, but as a novice developer, I've never encountered a scenario like the one I've found myself in.</p>

<p>Essentially, I'm using the attachment-fu plugin on two levels.</p>

<ol>
<li>Is for user avatars in the user class.</li>
<li>Is to allow file attachments (<strong><em>PDFs</em></strong>, etc) in a messaging system.</li>
</ol>

<p>My question is what the best use practice would be in these situations to remain <strong><em>DRY</em></strong>, clear, and consistent.</p>

<p>Clearly it would make no sense to define and execute the plugin in both classes, but there's something deeply strange to me (possibly unfounded) about just going ahead and setting it all up in the godly Application class.</p>

<p>Is there something in between, or is the parent class the way to go?</p>

<p>Thanks!</p>

[{'answer_id': 20001, 'author': 'John Topley', 'author_id': 1450, 'author_profile': 'https://Stackoverflow.com/users/1450', 'pm_score': 2, 'selected': False, 'text': '<p>Is "outsourcing" avatar support entirely to <a href="http://en.gravatar.com/" rel="nofollow noreferrer">Gravatar</a> an option? There are some Rails plugins that will display avatars hosted by Gravatar. You might not need to re-invent the wheel there.</p>\n'}, {'answer_id': 20029, 'author': 'wfarr', 'author_id': 2286, 'author_profile': 'https://Stackoverflow.com/users/2286', 'pm_score': 2, 'selected': True, 'text': '<p>I would lean towards using a parent class, with subclassing for the different ways you intend to actually use the attachments in your application. It may not be the DRYest solution available, however, it lends itself to a logical pattern rather well.</p>\n'}, {'answer_id': 78671, 'author': 'Dave Smylie', 'author_id': 1505600, 'author_profile': 'https://Stackoverflow.com/users/1505600', 'pm_score': 2, 'selected': False, 'text': "<p>What's the DRY issue with defining the attachment_fu settings twice?</p>\n\n<p>Unless the files are of the same type and being stored in the same place, you're not going to be repeating anything in the configuration. </p>\n\n<p>Sure, you'll have two has_attachment declarations, but the options will mostly differ (one declaration for your avatars and the other for your pdf's etc.</p>\n\n<p>99.99% of the code to handle attachment will be buried in the attachment_fu libs, your configuration code should be pretty DRY by default =)</p>\n"}, {'answer_id': 78719, 'author': 'Dan Harper', 'author_id': 14530, 'author_profile': 'https://Stackoverflow.com/users/14530', 'pm_score': 0, 'selected': False, 'text': '<p>Couldn\'t you use <a href="http://wiki.rubyonrails.org/rails/pages/UnderstandingPolymorphicAssociations" rel="nofollow noreferrer">Polymorphic Associations</a>?</p>\n\n<p>I\'m about to hit this in my app with attachment_fu, so I\'m not exactly sure on attachment_fu, but for the old school <a href="http://wiki.rubyonrails.org/rails/pages/File+Column+Plugin" rel="nofollow noreferrer">File Column</a> plugin, I would use Polymorphic Associations.</p>\n\n<p>My "file" model would be:</p>\n\n<pre><code>    class FileUpload &lt; ActiveRecord::Base\n      belongs_to :fileable, :polymorphic =&gt; true\n      file_column :name\n    end\n</code></pre>\n\n<p>and then any models that needed a file attachment would be like:</p>\n\n<pre><code>    class Company &lt; ActiveRecord::Base\n      has_many :file_uploads, :as =&gt; :fileable\n    end\n</code></pre>\n\n<p>File Column is no good anymore as it borks on Safari 3.x and is no longer maintained.  It was nice and simple though... Ah, the good old days...</p>\n'}, {'answer_id': 80016, 'author': 'Zach', 'author_id': 9128, 'author_profile': 'https://Stackoverflow.com/users/9128', 'pm_score': 2, 'selected': False, 'text': '<p>What wfarr is describing would be <a href="http://apidock.com/rails/ActiveRecord/Base" rel="nofollow noreferrer">single table inheritance</a>, which is what I currently do in this situation. I have one table for Assets which contains all the necessary attachment_fu columns, plus an extra column called type, which will hold the actual model name. I have a model for assets and additional models for specific upload types that inherit from assets:</p>\n\n<p>asset.rb:</p>\n\n<pre><code>class Asset &lt; ActiveRecord::Base\n  ... attachment_fu logic ...\nend\n</code></pre>\n\n<p>avatar.rb:</p>\n\n<pre><code>class Avatar &lt; Asset\n  ... avatar specific attachment_fu logic ...\nend\n</code></pre>\n\n<p>pdf.rb:</p>\n\n<pre><code>class PDF &lt; Asset\n  ... PDF specific attachment_fu logic ...\nend\n</code></pre>\n'}, {'answer_id': 472712, 'author': 'The Pied Pipes', 'author_id': 52092, 'author_profile': 'https://Stackoverflow.com/users/52092', 'pm_score': 0, 'selected': False, 'text': '<p>For what it\'s worth, I think Patrick Berkeley has done a good job with handling multiple attachments through the Paperclip plugin. He\'s outlined his work here:</p>\n\n<p><a href="http://gist.github.com/33011" rel="nofollow noreferrer">http://gist.github.com/33011</a></p>\n'}]
['https://Stackoverflow.com/questions/19988', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2293/']