Forum: Ruby on Rails attachment_fu - more then one attachment in model?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
866edcb271e88be4bfd935eea7947730?d=identicon&s=25 Bojan Mihelac (Guest)
on 2007-03-07 22:43
(Received via mailing list)
Is it possible to have more than one attachment in model using
attachment_fu?

thanks,
Bojan
--
Bojan Mihelac
-> Ruby on Rails and Web Developer Blog : http://source.mihelac.org
866edcb271e88be4bfd935eea7947730?d=identicon&s=25 Bojan Mihelac (Guest)
on 2007-03-08 14:54
(Received via mailing list)
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

--
Bojan Mihelac
-> Ruby on Rails and Web Developer Blog : http://source.mihelac.org
00973881979aa0a660ffbbb2f7a907fb?d=identicon&s=25 Peter De Berdt (Guest)
on 2007-03-08 16:59
(Received via mailing list)
On 08 Mar 2007, at 14:53, 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

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
866edcb271e88be4bfd935eea7947730?d=identicon&s=25 Bojan Mihelac (Guest)
on 2007-03-08 20:55
(Received via mailing list)
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 Mihelac
-> Ruby on Rails and Web Developer Blog : http://source.mihelac.org
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2007-03-08 21:00
(Received via mailing list)
> 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
http://weblog.techno-weenie.net
http://mephistoblog.com
866edcb271e88be4bfd935eea7947730?d=identicon&s=25 Bojan Mihelac (Guest)
on 2007-03-08 21:28
(Received via mailing list)
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...

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

best regards,
Bojan

--
Bojan Mihelac
-> Ruby on Rails and Web Developer Blog : http://source.mihelac.org
00973881979aa0a660ffbbb2f7a907fb?d=identicon&s=25 Peter De Berdt (Guest)
on 2007-03-08 22:02
(Received via mailing list)
On 08 Mar 2007, at 20:59, Rick Olson 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
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2007-03-08 22:09
(Received via mailing list)
>
> 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 :)

--
Rick Olson
http://weblog.techno-weenie.net
http://mephistoblog.com
866edcb271e88be4bfd935eea7947730?d=identicon&s=25 Bojan Mihelac (Guest)
on 2007-03-08 22:53
(Received via mailing list)
Rick Olson 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 :)

acts_as_paranoid Rick ;)



--
Bojan Mihelac
-> Ruby on Rails and Web Developer Blog : http://source.mihelac.org
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2007-03-08 23:02
(Received via mailing list)
> > 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 :)
>
> acts_as_paranoid Rick ;)

That's mine too.


--
Rick Olson
http://weblog.techno-weenie.net
http://mephistoblog.com
866edcb271e88be4bfd935eea7947730?d=identicon&s=25 Bojan Mihelac (Guest)
on 2007-03-08 23:21
(Received via mailing list)
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 :)
>> acts_as_paranoid Rick ;)
>
> That's mine too.
>
>

but joke was mine :)

--
Bojan Mihelac
-> Ruby on Rails and Web Developer Blog : http://source.mihelac.org
A587929f80f37c163f855f5610f2aa0c?d=identicon&s=25 gberz3 (Guest)
on 2007-04-17 17:31
(Received via mailing list)
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?
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2007-04-17 18:06
(Received via mailing list)
On 4/17/07, gberz3 <gberz3@gmail.com> 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 Olson
http://lighthouseapp.com
http://weblog.techno-weenie.net
http://mephistoblog.com
A587929f80f37c163f855f5610f2aa0c?d=identicon&s=25 gberz3 (Guest)
on 2007-04-17 18:38
(Received via mailing list)
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
34a7615f38496a5dafbb3e6b721c435e?d=identicon&s=25 Mohit Sindhwani (Guest)
on 2007-04-17 18:44
(Received via mailing list)
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 :) 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.

>   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 :) 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.
A587929f80f37c163f855f5610f2aa0c?d=identicon&s=25 gberz3 (Guest)
on 2007-04-17 18:51
(Received via mailing list)
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.  ;)

Regards,
Michael
34a7615f38496a5dafbb3e6b721c435e?d=identicon&s=25 Mohit Sindhwani (Guest)
on 2007-04-17 19:14
(Received via mailing list)
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.  ;)
>
>
Haven't used attachment_fu, so can't entertain your curiousity, sorry :P

> Regards,
> Michael
>
>
Cheers
Mohit.
4715ae8a9ac5152600ccd3012267ab6b?d=identicon&s=25 James Stewart (Guest)
on 2007-04-17 20:49
(Received via mailing list)
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 Stewart
Play: http://james.anthropiccollective.org
Work: http://jystewart.net/process/
50bc8ae59e4280e2ac14b732530e4d17?d=identicon&s=25 Rob B (Guest)
on 2007-06-12 00:40
Rick Olson wrote:
> On 4/17/07, gberz3 <gberz3@gmail.com> 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 Olson
> 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| %>
          <p><%= f.file_field :uploaded_data %>
        <% end %>

this is generating the below:
     <p><input id="property_image_uploaded_data"
name="property_image[uploaded_data]" size="30" type="file" />

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

Any help most appreciated.

Rob
This topic is locked and can not be replied to.