Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Adding Attachments to Other Models

Has anyone had success adding the document class to other models? I'm trying to do it with a polymorphic association, like:

document.rb

belongs_to :attachable, polymorphic: true

lesson.rb (another model)

has_many :documents, as: :attachable, dependent: :destroy

Then trying in my lesson form to add this:

<%= f.fields_for :document do |document_fields| %>
  <%= document_fields.input :attachment, as: :file %>
<% end %>

I'm getting multiple errors. Anyone done this, or know of a way to do this? I want to be able to attach files to other models, so a polymorphic association is best.

I've added columns to the document model

attachable_id: integer
attachable_type:string

Any ideas or places to look would be great! I'm struggling quite a bit here. Thanks in advance!

1 Answer

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

It's been a little while but I think you need to do f.fields_for :documents.