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 M.
→ Ruby on Rails and Web D. Blog : http://source.mihelac.org

Bojan M. 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


Bojan M.
→ Ruby on Rails and Web D. Blog : http://source.mihelac.org

On 08 Mar 2007, at 14:53, Bojan M. 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

  1. One-to-many

class MyModel < ActiveRecord::Base
has_many :attachments
end

class Attachment < ActiveRecord::Base
belongs_to :my_model
end

  1. 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:

Bojan
attachments, a simple one to many relationship if all attachments are to

*3. Or you can just add two integer fields to the model you want to
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


Bojan M.
→ Ruby on Rails and Web D. Blog : http://source.mihelac.org

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 O.
http://weblog.techno-weenie.net
http://mephistoblog.com

Rick O. 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…

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

best regards,
Bojan


Bojan M.
→ Ruby on Rails and Web D. Blog : http://source.mihelac.org

On 08 Mar 2007, at 20:59, Rick O. wrote:

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.

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

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 O.
http://weblog.techno-weenie.net
http://mephistoblog.com

Rick O. wrote:

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:


Bojan M.
→ Ruby on Rails and Web D. Blog : http://source.mihelac.org

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 O.
http://weblog.techno-weenie.net
http://mephistoblog.com

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?

Rick O. 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:


Bojan M.
→ Ruby on Rails and Web D. Blog : http://source.mihelac.org

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

On 4/17/07, gberz3 [email protected] wrote:

into the database. Is attachment_fu for me?
ActiveRecord provides this functionality out of the box:

class User < ActiveRecord::Base
has_many :photos
end


Rick O.
http://lighthouseapp.com
http://weblog.techno-weenie.net
http://mephistoblog.com

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!

just using it for effect)

  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.

  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.

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.

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.

  1. 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.

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

On Apr 17, 2007, at 12:37 PM, 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’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

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/


James S.
Play: http://james.anthropiccollective.org
Work: Processing Greenbelt 2009 – James Stewart

Rick O. wrote:

On 4/17/07, gberz3 [email protected] wrote:

into the database. Is attachment_fu for me?
ActiveRecord provides this functionality out of the box:

class User < ActiveRecord::Base
has_many :photos
end


Rick O.
http://lighthouseapp.com
http://weblog.techno-weenie.net
http://mephistoblog.com

Hi,

Any change someone could give a few tips on the controller code ie
create where attachment_fu is being used to store multiple (child or
belongs_to) records.

I’ve been trying to adapt the code in this tutorial:
http://railsforum.com/viewtopic.php?pid=3713

  1. def create
  2. @project = Project.new(params[:project])
  3. @task = @project.tasks.build(params[:task])
  4. if @project.save
  5. redirect_to :action => 'index'
    
  6. else
  7. render :action => 'new'
    
  8. end
  9. end

So I’m guessing the required code would be close to the above but
swapping tasks for “images” in this case.

My hopeless attempt so far is:

def create
@property = Property.new(params[:property])
@property_image =
@property.property_image.build(params[:uploaded_data])
if @property.save
flash[:notice] = ‘Property was successfully created.’
redirect_to :action => ‘list’
else
render :action => ‘new’
end
end

Where property_image is the belongs_to/child record of property.

In my form, because the main form is about adding Property objects, I’m
using
the below for the attachment images…

<% fields_for “property_image” do |f| %>

<%= f.file_field :uploaded_data %>
<% end %>

this is generating the below:

seems I might be ok on the view, but not the controller code ?

Any help most appreciated.

Rob