# Does acts\_as\_attachment :storage =\> :db\_system work?

**URL:** <https://www.ruby-forum.com/t/does-acts-as-attachment-storage-db-system-work/67949>\
**Category:** Rails\
**Created:** [September 8, 2006, 3:38am UTC](https://www.ruby-forum.com/t/does-acts-as-attachment-storage-db-system-work/67949 "2006-09-08T03:38:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cynthia\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@Cynthia\_K](https://www.ruby-forum.com/u/Cynthia_K)\
**Post date:** [September 8, 2006, 3:38am UTC](https://www.ruby-forum.com/t/does-acts-as-attachment-storage-db-system-work/67949/1 "2006-09-08T03:38:38Z")

</div>

I found the post about needing to install acts\_as\_attachement\_1\_1\_6 if  
you are not on edge rails. But I am having trouble getting db\_system  
storage to work. As far as I can tell in from the database, things are  
working, but I don’t see how my model table hooks up with the db\_files  
table so I am having trouble altering the image\_tag from the tutorial  
([http://weblog.techno-weenie.net/articles/acts\_as\_attachment](http://weblog.techno-weenie.net/articles/acts_as_attachment)) so that  
it pulls data from the database. (Everything works fine if I want to  
use :storage =\> :file\_system)

How are my generated model and db\_files connected? and what do I need  
in my show action to send the image to the browser? I tried code like  
this:

```
send_data @picture.data_file.data, :filename => @picture.filename,

```

:type =\> @picture.content\_type, :dispo  
sition =\> ‘inline’

But I get an undefined method error for data\_file, which doesn’t  
surprise me since I don’t see any link between the db\_files table and  
my pictures table. Parent\_id could have been it, but a) it is blank  
and b) the docs seem to indicate that is used mainly for keeping track  
of which images are just resized versions of other images.

TIA

–  
Cynthia K.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Cynthia\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@Cynthia\_K](https://www.ruby-forum.com/u/Cynthia_K)\
**Post date:** [September 8, 2006, 4:54am UTC](https://www.ruby-forum.com/t/does-acts-as-attachment-storage-db-system-work/67949/2 "2006-09-08T04:54:46Z")

</div>

> ```
> send_data @picture.data_file.data, :filename => @picture.filename,
> 
> ```
> 
> :type =\> @picture.content\_type, :dispo  
> sition =\> ‘inline’

Shouldn’t that be db\_file, not data\_file?

–  
Rick O.  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![Cynthia\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@Cynthia\_K](https://www.ruby-forum.com/u/Cynthia_K)\
**Post date:** [September 8, 2006, 9:17am UTC](https://www.ruby-forum.com/t/does-acts-as-attachment-storage-db-system-work/67949/3 "2006-09-08T09:17:22Z")

</div>

> > ```
> > send_data @picture.data_file.data, :filename => @picture.filename,
> > 
> > ```
> > 
> > :type =\> @picture.content\_type, :dispo sition =\> ‘inline’
> 
> Shouldn’t that be db\_file, not data\_file?

OOps. yes. But that just gets me to a different error message:

You have a nil object when you didn’t expect it!  
The error occured while evaluating nil.data

And while trying to double check that I had not messed anything up, I  
am reminded that to get  
as far as I did, I had had to comment out another piece of code that  
was causing errors. So for a full recap:

I have Rails 1.1.6 installed on a RHEL 4 system running ruby 1.8.4  
(installed from source). I installed acts\_as\_attachment using the  
command “script/plugin install  
[http://svn.techno-weenie.net/projects/plugins/tags/acts\_as\_attachment-1.1.6](http://svn.techno-weenie.net/projects/plugins/tags/acts_as_attachment-1.1.6)”  
If I set the DB environment variable, running the test “rake  
test\_plugins PLUGIN=acts\_as\_attachment” seems to work - but I had  
expected more output - such as a list of # of tests and # passed.

If I set my Picture model up to use :storage =\> :file\_system, all  
seems to work just fine. To try to use :db\_system, I needed to  
uncomment the lines about db\_file before running the migration built  
when I did “script/generate attachment\_model picture”. Then I set up  
my model up like:

class Picture \< ActiveRecord::Base  
acts\_as\_attachment :storage =\> :db\_system, :max\_size =\>  
500.kilobytes, :content\_type =\> :image  
validates\_as\_attachment  
end

and my controller like this:

class PictureController \< ApplicationController  
def index  
@pictures = Picture.find(:all)  
end

def new  
@picture = Picture.new  
end

def show  
@picture = Picture.find(params[:id])  
send\_data @picture.db\_file.data, :filename =\> @picture.filename,  
:type =\> @picture.content\_type, :disposition =\> ‘inline’  
end

def create  
@picture = Picture.create params[:picture]  
redirect\_to :action =\> ‘show’, :id =\> @picture  
rescue ActiveRecord::RecordInvalid  
render :action =\> ‘new’  
end  
end

When I try to upload a new image, I can see the following exception:

undefined method `db\_file\_id=’ for #Picture:0xb77f83fc

RAILS\_ROOT: /rails/admissions/script/…/config/…  
Application Trace | Framework Trace | Full Trace

/software/stow/ruby-1.8.4/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active\_record/base.rb:1789:in  
`method_missing' #{RAILS_ROOT}/vendor/plugins/acts_as_attachment-1.1.6/lib/technoweenie/acts_as_attachment.rb:316:in `save\_to\_storage’  
/software/stow/ruby-1.8.4/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active\_record/callbacks.rb:333:in  
`callback' /software/stow/ruby-1.8.4/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/callbacks.rb:330:in `callback’  
/software/stow/ruby-1.8.4/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active\_record/callbacks.rb:248:in  
`create_or_update' /software/stow/ruby-1.8.4/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/base.rb:1392:in `save\_without\_validation’  
/software/stow/ruby-1.8.4/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active\_record/validations.rb:724:in  
`save_without_transactions' /software/stow/ruby-1.8.4/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/transactions.rb:126:in `save’  
/software/stow/ruby-1.8.4/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active\_record/connection\_adapters/abstract/database\_statements.rb:51:in  
`transaction' /software/stow/ruby-1.8.4/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/transactions.rb:91:in `transaction’  
/software/stow/ruby-1.8.4/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active\_record/transactions.rb:118:in  
`transaction' /software/stow/ruby-1.8.4/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active_record/transactions.rb:126:in `save’  
/software/stow/ruby-1.8.4/lib/ruby/gems/1.8/gems/activerecord-1.14.3/lib/active\_record/base.rb:408:in  
`create' #{RAILS_ROOT}/app/controllers/picture_controller.rb:16:in `create’

In the module DbFileMethods section of acts\_as\_attachment.rb, I  
commented out the line that calls db\_file\_id= and then I can upload  
files. I see the insert statements for both db\_files and pictures and  
looking in the database, I see roughly the data I expect (Can’t easily  
inspect the image from the terminal, but it is of the same length as  
the length stored in pictures.size. But when I try to use the show  
method above, I get the nil.data error at the top of this post. And I  
am having trouble working backwards from the only code that seems it  
ties pictures to db\_files to the syntax I need in my show controller  
so I can display the images right out of the database. Is this the  
code that tells the the two tables they are related?

```
module DbFileMethods def self.included(base) #:nodoc:
    base.belongs_to :db_file
    base.before_save :save_to_storage # so the db_file_id can be set
  end

```

And I would have expected one or the other table to have a foreign  
key. What am I missing?

–  
Cynthia K.  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Cynthia\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@Cynthia\_K](https://www.ruby-forum.com/u/Cynthia_K)\
**Post date:** [September 8, 2006, 2:46pm UTC](https://www.ruby-forum.com/t/does-acts-as-attachment-storage-db-system-work/67949/4 "2006-09-08T14:46:09Z")

</div>

undefined method `db\_file\_id=’ for #Picture:0xb77f83fc

That means you’re missing the db\_file\_id row. This points your  
attachment to the row in the db\_files table. Removing the part that  
sets this in the plugin means the two are never attached.

–  
Rick O.  
[http://weblog.techno-weenie.net](http://weblog.techno-weenie.net)  
[http://mephistoblog.com](http://mephistoblog.com)

---

<div class="post-metadata">

**Author:** ![Cynthia\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/db5fbb/32.png) [@Cynthia\_K](https://www.ruby-forum.com/u/Cynthia_K)\
**Post date:** [September 8, 2006, 6:12pm UTC](https://www.ruby-forum.com/t/does-acts-as-attachment-storage-db-system-work/67949/5 "2006-09-08T18:12:32Z")

</div>

Thanks Rick. I added the column to my created table and now everything  
works. Can I ask that you patch  
acts\_as\_attachment-1.1.6/generators/attachment\_model/templates/migration.rb  
to have the following lines somewhere within the create table  
definition:

```
  # uncomment this before migrating if you want db-based files
  # t.column "db_file_id", :integer

```

## Thanks. Great product, now that you showed me the trick to making db\_system work.

Cynthia K.  
removed\_email\_address@domain.invalid
