M:N Association (has_and_belongs_to many)

13 views
Skip to first unread message

Andreas

unread,
May 28, 2009, 4:12:30 AM5/28/09
to Paperclip Plugin
Dear Group,

I have found paperclip while searching for a rails plugin for
attaching files. It looks quite great.

It seemed as if with paperclip it is only possible to build a 1:1
assoziation or perhaps a 1:n assoziation (as described in the multiple
attachments tutorail linked to on github).

Is there any possibility to create a m:n assoziation, where I only
need to upload a picture (or other file) once and can attach it to
many objects (and also - but this seems to be easier - to have many
attachments per object)?

Many thanks for your answers.

Kennon Ballou

unread,
May 28, 2009, 4:29:37 AM5/28/09
to papercli...@googlegroups.com
Hi Andreas,

Paperclip doesn't create a separate model, it "attaches" to an existing
one. You can have as many paperclip attachments on a model as you like,
but they have to be distinctly named.

To do what you're trying to do, you can create a model with a paperclip
attachment, and then have that model M:N joined to other models.

Like this:

class Gallery < ActiveRecord::Base
has_many :gallery_pictures
has_many :pictures, :through => :gallery_pictures
end

class GalleryPicture < ActiveRecord::Base
belongs_to :gallery
belongs_to :picture
end

class Picture < ActiveRecord::Base
has_many :gallery_pictures
has_many :galleries, :through => :gallery_pictures

has_attachment :image
end

You can then do things like this:

<% @gallery.pictures.each do |picture| %>
<%= image_tag picture.image.url %>
<% end %>

You could even get a little jiggy and make a join table with a
polymorphic association and attach pictures to literally anything.

The point is, paperclip (in this case, the :image) doesn't concern
itself with associations or how your model structure works (this is one
of the big advantages of paperclip over e.g. attachment_fu). Paperclip
attachments really are just clipped onto an existing model like, well,
clipping a note onto a page with a paperclip :-)

| Kennon Ballou
| Angry Turnip, Inc.

Andreas

unread,
May 28, 2009, 5:56:41 AM5/28/09
to Paperclip Plugin
Hi Kennon,

thanks for the very quick answer. I am quite new to rails, so I try to
say it with my own words.

The Picture Model only acts as a kind of "container" for uptaking the
image (attachment), because Paperclip always needs an explicit model
to attach to.
So while attaching the images to the Picture model for each image
(attachment) exactly one Picture object is created, which now can be
used by other models.

By polymorph you mean something like that???
Like this:

class Gallery < ActiveRecord::Base
has_many :gallery_pictures, :as => attachable
has_many :pictures, :through => :gallery_pictures
end

here ist the "as" added

class Product < ActiveRecord::Base
has_many :gallery_pictures, :as => attachable
has_many :pictures, :through => :gallery_pictures
end

this is one hypothtic new model to attach to - the Product model

class GalleryPicture < ActiveRecord::Base
belongs_to :picture
belongs_to :attachable, :polymorphic => true
end

the changed join table (I deleted the rows that mention namele the
gallery and the product model)

class Picture < ActiveRecord::Base
has_many :gallery_pictures
has_many :galleries, :through => :gallery_pictures
has_many :products, :through => :gallery_pictures

has_attachment :image
end

I added the product model here (Or should I delete both lines??)



So, I would like to hear if I understood you correctly. Many Thanks!!!


Andreas

Kennon Ballou

unread,
May 28, 2009, 6:14:33 AM5/28/09
to papercli...@googlegroups.com
Hi Andreas,

I think the explanation in your own words of how paperclip works is correct.

With the polymorphic objects, you're almost there, but I think it should
be more like this:

class AttachedPicture < ActiveRecord::Base
belongs_to :attached, :polymorphic => true
belongs_to :picture
end

class Gallery < ActiveRecord::Base
has_many :attached_pictures, :as => :attached
has_many :pictures, :through => :attachable_pictures
end

class Picture < ActiveRecord::Base
has_many :attachable_pictures
has_attachment :image
end

I'm not sure, however, if you could have a Picture::has_many :galleries
unless you specify a condition that deals with attachable_type, etc.

If you really want to go crazy, though, you could specify a double
polymorphic join table that can join anything to anything else. This
would be very flexible, but it might get a little ridiculous, and you'd
probably run into more issues with some of the associations :-)

| Kennon Ballou
| Angry Turnip, Inc.

Andreas wrote:

Andreas

unread,
May 28, 2009, 8:48:27 AM5/28/09
to Paperclip Plugin

Hi Kennon,

many thanks again.

Well, I think the double polymorphic Association is not necessary; but
an interesting issue :-). Quite crazy!

In the next time I will check this out if it works for my case.

Many thanks and best regards.

Andreas

Andreas

unread,
May 28, 2009, 12:03:44 PM5/28/09
to Paperclip Plugin

Hi Kennon,

at Github I found polymorphicpaperclip as a plugin. Here is a short
tutorial for it.

http://burm.net/2008/10/17/ruby-on-rails-polymorphic-paperclip-plugin-tutorial/

I thought it could be of your or another one´s interest.

What do you think about it?

Looks a bit magic and not too detailed described in my opinion.

Andreas

unread,
Jun 8, 2009, 4:39:45 AM6/8/09
to Paperclip Plugin
Dear community,

here are two links to very helpful sites for polymorphic use of
paperclip or any other database models.

http://blog.hasmanythrough.com/2006/4/3/polymorphic-through

http://m.onkey.org/2007/8/14/excuse-me-wtf-is-polymorphs

Many thanks, Kennon!


Best regards

Andreas
Reply all
Reply to author
Forward
0 new messages