file_column outside of form use

I’m having trouble wrapping my head around how to do this.

If I have photo and photoset models with an :image file_column in photo, how do I process a directory of photos, rather than one at at time through a form, what’s the syntax?

ps = Photoset.new
Dir.glob(’*.jpg’).each do |f|
p = Photo.new
p.image = f <--------- This is the linkage I don’t believe is this easy?
ps.images << p
end

Anyone else done something like this? Google and list archive search haven’t done it for me.

Fluffy Hippo wrote:

  p.image = f <--------- This is the linkage I don't believe is this easy?

It might be that easy. I would test. There is a part of file_column called file_compat.rb that has a comment which says this:

# This bit of code allows you to pass regular old files to
# file_column. file_column depends on a few extra methods that the
# CGI uploaded file class adds. We will add the equivalent methods
# to file objects if necessary by extending them with this module. This
# avoids opening up the standard File class which might result in
# naming conflicts.

So give it a try. It might just work. The comments in the plugin indicate it should be supported.

Eric

Almost that easy … you have to pass a File object instead of a string and then it works fine.

Using your variables …

fh = File.open(f)
p.image = fh
p.save