attachment_fu - more then one attachment in model?

Is it possible to have more than one attachment in model using attachment_fu?

thanks,
Bojan

Bojan Mihelac wrote:

Is it possible to have more than one attachment in model using attachment_fu?

thanks,
Bojan

or can you recommend good, simple extensible upload plugin

Bojan

Instead of trying to have more than one attachment in a single model, make your attachments a related table.

You can then use single table inheritance for your different attachments, a simple one to many relationship if all attachments are to be treated the same or if you need a fixed number of attachments, put your foreign keys in your model. It depends on what you want to do.

1. Single Table Inheritance

class Attachment < ActiveRecord::Base

belongs_to :my_model

end

class FirstAttachment < Attachment

end

2. One-to-many

class MyModel < ActiveRecord::Base

has_many :attachments

end

class Attachment < ActiveRecord::Base

belongs_to :my_model

end

3. Or you can just add two integer fields to the model you want to store your multiple attachments in:

class MyModel < ActiveRecord::Base

belongs_to :first_attachment, :class_name => “Attachment”, :foreign_key => “first_attachment_id”

belongs_to :second_attachment, :class_name => “Attachment”, :foreign_key => “second_attachment_id”

end

Best regards

Peter De Berdt

Peter De Berdt wrote:

Is it possible to have more than one attachment in model using

attachment_fu?

thanks,

Bojan

or can you recommend good, simple extensible upload plugin

Instead of trying to have more than one attachment in a single model, make your attachments a related table.

You can then use single table inheritance for your different attachments, a simple one to many relationship if all attachments are to be treated the same or if you need a fixed number of attachments, put your foreign keys in your model. It depends on what you want to do.

*1. Single Table Inheritance*
class Attachment < ActiveRecord::Base
  belongs_to :my_model
end

class FirstAttachment < Attachment
end

*2. One-to-many*

class MyModel < ActiveRecord::Base
  has_many :attachments
end

class Attachment < ActiveRecord::Base
  belongs_to :my_model
end

*3. Or you can just add two integer fields to the model you want to store your multiple attachments in:*

class MyModel < ActiveRecord::Base
  belongs_to :first_attachment, :class_name => "Attachment", :foreign_key => "first_attachment_id"
  belongs_to :second_attachment, :class_name => "Attachment", :foreign_key => "second_attachment_id"
end

Best regards

Peter De Berdt

Peter, thanks for answer. It is good and clean approach, but looks like overkill for simple case that I described (one image and one document). Besides need for creating new models it would also complicate writing administration interface which would be so easy if attachment_fu allowed many attachments per model.

best regards,
Bojan

Peter, thanks for answer. It is good and clean approach, but looks like
overkill for simple case that I described (one image and one document).
Besides need for creating new models it would also complicate writing
administration interface which would be so easy if attachment_fu allowed
many attachments per model.

Where would you store the data then? I looked at how file_column
stores its data and thought that the attachment info is just begging
to be its own model. I get things like "multiple attachments per
model" for free through rails associations then.

Rick Olson wrote:

Peter, thanks for answer. It is good and clean approach, but looks like
overkill for simple case that I described (one image and one document).
Besides need for creating new models it would also complicate writing
administration interface which would be so easy if attachment_fu allowed
many attachments per model.

Where would you store the data then? I looked at how file_column
stores its data and thought that the attachment info is just begging
to be its own model. I get things like "multiple attachments per
model" for free through rails associations then.

Hi Rick, I just checked once again code of attachment_fu and the main concerns I feel is that generating admin interface would be harder in case when multiple attachments are not needed.

Maybe I am wrong and maybe it would be to complicated to implement all callbacks and before|after methods, but it still seems to me it would be nice to have option to say:

has_attachment :name => 'resume', :content_type => ['application/pdf']
has_attachment :name => 'icon', :resize_to => [50,50]

Db table could have fields resume_filename, icon_filename, icon_content_type and so on...

Anyway, I think it is great work you done with attachment_fu.

best regards,
Bojan

I strongly agree with Rick.

Looking at it from an OO point-of-view, you must agree that an attachment is a class of its own (with its properties like content_type, size, …), and your model just stores references to the instances of your attachment class (hence Attachment should have its own model). And the Rails syntax couldn’t make it any simpler for you to get the related record in one nice query:

@my_object=MyModel.find(:id, :include => [:attachment1, :attachment2])

You can then just call them just like you would any other field:

@my_object.attachment1,@my_object.attachment2

Best regards

Peter De Berdt

Hi Rick, I just checked once again code of attachment_fu and the main
concerns I feel is that generating admin interface would be harder in
case when multiple attachments are not needed.

Maybe I am wrong and maybe it would be to complicated to implement all
callbacks and before|after methods, but it still seems to me it would be
nice to have option to say:

has_attachment :name => 'resume', :content_type => ['application/pdf']
has_attachment :name => 'icon', :resize_to => [50,50]

Db table could have fields resume_filename, icon_filename,
icon_content_type and so on...

How would this solve the UI problem? You're just storing the data
slightly differently. You still need actions for
creating/updating/deleting the attachments.

Anyways, uploading isn't hard, you can just cannibalize my plugin and
rework it the way you want too. Just please stay away from the
attachment_fu name :slight_smile:

Rick Olson wrote:

Hi Rick, I just checked once again code of attachment_fu and the main
concerns I feel is that generating admin interface would be harder in
case when multiple attachments are not needed.

Maybe I am wrong and maybe it would be to complicated to implement all
callbacks and before|after methods, but it still seems to me it would be
nice to have option to say:

has_attachment :name => 'resume', :content_type => ['application/pdf']
has_attachment :name => 'icon', :resize_to => [50,50]

Db table could have fields resume_filename, icon_filename,
icon_content_type and so on...

How would this solve the UI problem? You're just storing the data
slightly differently. You still need actions for
creating/updating/deleting the attachments.

It would be very easy to extend scaffold to allows attachment. Sure, I am going here with easiest way-out, but my problem is really one that looks to be solved easily. I am afraid that attachment_fu won't solve my problem this time, but surely this plugin would be great help for all others scenarios.

Thanks to you and Peter for taking time to help.

Anyways, uploading isn't hard, you can just cannibalize my plugin and
rework it the way you want too. Just please stay away from the
attachment_fu name :slight_smile:

acts_as_paranoid Rick :wink:

> Anyways, uploading isn't hard, you can just cannibalize my plugin and
> rework it the way you want too. Just please stay away from the
> attachment_fu name :slight_smile:

acts_as_paranoid Rick :wink:

That's mine too.

Rick Olson wrote:

Anyways, uploading isn't hard, you can just cannibalize my plugin and
rework it the way you want too. Just please stay away from the
attachment_fu name :slight_smile:

acts_as_paranoid Rick :wink:

That's mine too.

but joke was mine :slight_smile:

This thread slightly confused me. My problem is similar but
different. I need the ability to upload an arbitrary number of
related photos. For instance, say a client has an accident and needs
to file a claim. They want to be able to initiate the claim and
upload as many photos related to that claim as they deem necessary to
explain the situation.

I suppose my main goal is simply to transparently be able to save the
files in the filesystem and link to them as opposed to chunking photos
into the database. Is attachment_fu for me?

ActiveRecord provides this functionality out of the box:

class User < ActiveRecord::Base
  has_many :photos
end

Yup, but does ActiveRecord have any mechanism by which to save the
photo files in a directory as opposed to the DB? I suppose that layer
of "transparency" is really the main thing I'm looking for.

For instance, one of my scenarios is as follows: Clients send e-mails
via their mobile phone. The e-mail is then parsed using ActionMailer,
and a few other choice items. At this point I need to save the body
of the e-mail to a record, then save any photo attachments to the file
system (I suppose similar to the way file_column does it).

Originally my plan was to manually do the following:

  1) Parse the email
  2) Save the body to the EmailBody model (that's not the real name,
just using it for effect)
  3) Save the file to the filesystem in a single directory using the
MD5 (and doing validation to make sure that files aren't overwritten/
repeated, etc)
  4) Associating the photos to the appropriate EmailBody with a
'belongs_to' in my PhotoAttachments model and of course a 'has_many'
in the EmailBody

. . .however, since coming upon 'attachment_fu' I might not need to do
all of that. So I suppose my main concerns are as follows:

  1) Can attachment_fu validate photos to ensure none repeat? Any type
of MD5 signature validation, etc?
  2) I don't need a form because this is handled in the background by
ActionMailer so how do I accommodate for :content_type, :thumbnails,
etc. . .? Manually?
  3) How does one handle file-type validations? For instance, if a
client includes photo and document attacments, I need to differentiate
yet still keep all attachments accessible.

. . .maybe I'm completely on the wrong path, and perhaps I've even
severely confused you. In either case I'd appreciate your input.

Thanks,
Michael

gberz3 wrote:

Yup, but does ActiveRecord have any mechanism by which to save the
photo files in a directory as opposed to the DB? I suppose that layer
of "transparency" is really the main thing I'm looking for.
  
I'll answer the simple question :slight_smile: Look at file_column - it should help you out!

For instance, one of my scenarios is as follows: Clients send e-mails
via their mobile phone. The e-mail is then parsed using ActionMailer,
and a few other choice items. At this point I need to save the body
of the e-mail to a record, then save any photo attachments to the file
system (I suppose similar to the way file_column does it).

Originally my plan was to manually do the following:

  1) Parse the email
  2) Save the body to the EmailBody model (that's not the real name,
just using it for effect)
  3) Save the file to the filesystem in a single directory using the
MD5 (and doing validation to make sure that files aren't overwritten/
repeated, etc)
  4) Associating the photos to the appropriate EmailBody with a
'belongs_to' in my PhotoAttachments model and of course a 'has_many'
in the EmailBody

. . .however, since coming upon 'attachment_fu' I might not need to do
all of that. So I suppose my main concerns are as follows:

  1) Can attachment_fu validate photos to ensure none repeat? Any type
of MD5 signature validation, etc?
  2) I don't need a form because this is handled in the background by
ActionMailer so how do I accommodate for :content_type, :thumbnails,
etc. . .? Manually?
  
Active Record validations apply whenever you use the model to save something to the database. It doesn't need to come from a form.

  3) How does one handle file-type validations? For instance, if a
client includes photo and document attacments, I need to differentiate
yet still keep all attachments accessible.

I don't remember finding a good solution for this, but I think you could check the extension of the file as a simple starting point?

. . .maybe I'm completely on the wrong path, and perhaps I've even
severely confused you. In either case I'd appreciate your input.

Maybe you are :slight_smile: but it's equally likely that I'm leading you up the wrong path too. I think my answer may provide the skeleton but you might need someone else to bring the meat.

Hope this helps!
Cheers
Mohit.

Thank you Mohit. Two issues:

1) I've used file_column before (only in practice, never production)
and it works great for single files, but I'm looking to be able to
associate any number of attachments (mostly photos) with a single
database entry (as per my example)

2) I've been seeing in the forumns that attachment_fu does what
file_column does and then some. . .hence my curiosity here. :wink:

Regards,
Michael

gberz3 wrote:

Thank you Mohit. Two issues:

1) I've used file_column before (only in practice, never production)
and it works great for single files, but I'm looking to be able to
associate any number of attachments (mostly photos) with a single
database entry (as per my example)
  

I'd think of having a table called EmailAttachments with a file_column field and emailBody_id as the foreign key. That way, you have one have_many relationship. Would that not work? Perhaps, you could also store other information in this on a per-file basis, if relevant. For example, you could store file type or something also in the same record.

2) I've been seeing in the forumns that attachment_fu does what
file_column does and then some. . .hence my curiosity here. :wink:

Haven't used attachment_fu, so can't entertain your curiousity, sorry :stuck_out_tongue:

Regards,
Michael

Cheers
Mohit.

Yup, but does ActiveRecord have any mechanism by which to save the

photo files in a directory as opposed to the DB? I suppose that layer

of “transparency” is really the main thing I’m looking for.

I’m not sure if this is what you’re getting at, but attachment_fu will let you save the uploaded data to file, the database, amazon S3, or if you want any other storage option that you can write a plugin for [1].

In one app I have products which can have many product images. I want to store those images in RAILS_ROOT/public/system/product_images so I declare my ProductImage model as:

class ProductImage < ActiveRecord::Base

has_attachment :file_system_path => ‘public/system/product_images/’

belongs_to :product

validates_as_attachment

end

. . .however, since coming upon ‘attachment_fu’ I might not need to do

all of that. So I suppose my main concerns are as follows:

  1. Can attachment_fu validate photos to ensure none repeat? Any type

of MD5 signature validation, etc?

  1. I don’t need a form because this is handled in the background by

ActionMailer so how do I accommodate for :content_type, :thumbnails,

etc. . .? Manually?

  1. How does one handle file-type validations? For instance, if a

client includes photo and document attacments, I need to differentiate

yet still keep all attachments accessible.

Those are some pretty specific requirements, so you’ll probably find you need to add/override some methods to make it work. It’d be pretty straightforward to add an ActiveRecord callback that makes sure you’re storing an MD5 signature each time you add a file, and then to add a validates_uniqueness_of call to make sure the images are unique within a given scope.

For #2, attachment_fu (and ActiveRecord) works the same way whether you’re calling it from a controller or any other context (one of the big gains of MVC) so as long as the parameters you’re passing in are the same as if you had used a form, you’ll be fine.

For #3, how would you do that in any database design? Maybe you want different types of associations, or maybe you could just use the content_type field that attachment_fu creates.

James.

1: See my write up about attachment_fu at http://jystewart.net/process/2007/03/rails-plugins-attachment_fu/