# Simple RoR problem with file\_column / rmagick

**URL:** <https://www.ruby-forum.com/t/simple-ror-problem-with-file-column-rmagick/126623>\
**Category:** Rails\
**Created:** [January 20, 2008, 4:15pm UTC](https://www.ruby-forum.com/t/simple-ror-problem-with-file-column-rmagick/126623 "2008-01-20T16:15:47Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aaaarg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9bcb4/32.png) [@Aaaarg](https://www.ruby-forum.com/u/Aaaarg)\
**Post date:** [January 20, 2008, 4:15pm UTC](https://www.ruby-forum.com/t/simple-ror-problem-with-file-column-rmagick/126623/1 "2008-01-20T16:15:47Z")

</div>

At least I suspect it’s a simple problem. I’m quite new.  
I have written a class to store images, and generate thumbnails for  
them. This works well.  
However I’ve been having a lot of trouble getting it to keep track of  
the files size.

1: class Photo \< ActiveRecord::Base  
2: file\_column :image, :magick =\> {  
3: :versions =\> {  
4: “thumb” =\> {  
5: :transformation =\> Proc.new { |image|  
6: image.change\_geometry!(‘160x120’) { |cols, rows,  
img|  
7: img.thumbnail!(cols, rows)  
8: }  
9: },  
10: :attributes =\> { :quality =\> 50 }  
11: }  
12: }  
13: }  
14: end

With rmagick it’s possible to access the size like so:  
Magick::ImageList.new(image\_address).filesize

So I was thinking of adding a method:  
def getsize  
Magick::ImageList.new(image).filesize #or should this be  
@image?  
end

Or perhaps this underneath line 5:  
@size = image.filesize

However both of these trigger errors. Could someone help?

Thanks!

---

<div class="post-metadata">

**Author:** ![Aaaarg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9bcb4/32.png) [@Aaaarg](https://www.ruby-forum.com/u/Aaaarg)\
**Post date:** [January 21, 2008, 6:47am UTC](https://www.ruby-forum.com/t/simple-ror-problem-with-file-column-rmagick/126623/2 "2008-01-21T06:47:52Z")

</div>

Aaaarg wrote:

> ```
> def getsize
> Magick::ImageList.new(image).filesize #or should this be
> 
> ```
> 
> @image?  
> end
> 
> Or perhaps this underneath line 5:  
> @size = image.filesize
> 
> However both of these trigger errors. Could someone help?

Try either

Magick::ImageList.new(image).cur\_image.filesize

or

Magick::Image.read(image).first.filesize

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![Aaaarg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9bcb4/32.png) [@Aaaarg](https://www.ruby-forum.com/u/Aaaarg)\
**Post date:** [January 21, 2008, 12:54pm UTC](https://www.ruby-forum.com/t/simple-ror-problem-with-file-column-rmagick/126623/3 "2008-01-21T12:54:33Z")

</div>

Thank you very much for the suggestions but it’s not quite what I’m  
looking for.  
I think what I’m having trouble with is more basic.

What I want to be able to is access the file size (and other  
attributes) outside, from an rhtml file.  
I have done this successfully by adding this line to the rhtml file:  
\<%= Magick::ImageList.new(photo.image).filesize %\>

Although this works, it incurs a massive time penalty, somewhere  
around 1 second per image.

I think the best method would be to get the size and store it when the  
image is saved, which is why I thought of putting this beneath line 5:  
@size = image.filesize

however that does nothing so far as I can tell.

I had another thought - defining this function at the start of the  
class:  
def store\_size(image)  
@size = image.filesize  
end

and adding this beneath line 5:  
store\_size(image)

however this gives the error ‘undefined method `write\_description’ for  
Photo:Class’

So how do I call a function on the object rather than the class from  
the block starting at line 5?

Thanks again!

---

<div class="post-metadata">

**Author:** ![Aaaarg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9bcb4/32.png) [@Aaaarg](https://www.ruby-forum.com/u/Aaaarg)\
**Post date:** [January 21, 2008, 1:08pm UTC](https://www.ruby-forum.com/t/simple-ror-problem-with-file-column-rmagick/126623/4 "2008-01-21T13:08:34Z")

</div>

add a feild ‘size’ to your table and this will work

class some\_class

before\_save :store\_size

def store\_size  
size = image.filesize  
end

…  
end

---

<div class="post-metadata">

**Author:** ![Aaaarg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9bcb4/32.png) [@Aaaarg](https://www.ruby-forum.com/u/Aaaarg)\
**Post date:** [January 21, 2008, 3:12pm UTC](https://www.ruby-forum.com/t/simple-ror-problem-with-file-column-rmagick/126623/5 "2008-01-21T15:12:07Z")

</div>

Hi, thanks for the reply. Still not quite what I’m looking for  
unfortunately.

I’ve posted the relevant code online for anyone willing to look:  
[Parked at Loopia](http://pastie.caboo.se/141484)

It is what I assumed would work, and I think it would except that when  
I call store\_image\_description from where I do, it tries to call it on  
the class, and not the object.

Any ideas?

---

<div class="post-metadata">

**Author:** ![Aaaarg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9bcb4/32.png) [@Aaaarg](https://www.ruby-forum.com/u/Aaaarg)\
**Post date:** [April 9, 2008, 2:29am UTC](https://www.ruby-forum.com/t/simple-ror-problem-with-file-column-rmagick/126623/6 "2008-04-09T02:29:53Z")

</div>

Already solved?

I had to patch file\_column a little bit. The Problem is, that you can’t  
access the image anywhere but on the filesystem. “image” holds only the  
name of the file. So 2 options:

1. Before store: load the file again into memory and get the size
2. Patch file\_column to call some “after\_render” method with the image,  
so you can do your info extraction.

You could insert this method in magic\_file\_column.rb in BaseUploaded

def call\_after\_render(img,version)  
if options[:after\_render]  
options[:after\_render].each do |sym|  
@instance.send(sym,img,version)  
end  
end  
end

now call this method from within transform\_with\_magick where apropriate.

in your model you should use sth like  
file\_column :image, :after\_render =\> [:myinfoextractor]  
private  
def myinfoextractor(img,version)  
self.width=img.columns  
end

I think that should give you some clue what to do…

---

<div class="post-metadata">

**Author:** ![Aaaarg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9bcb4/32.png) [@Aaaarg](https://www.ruby-forum.com/u/Aaaarg)\
**Post date:** [April 9, 2008, 6:50am UTC](https://www.ruby-forum.com/t/simple-ror-problem-with-file-column-rmagick/126623/7 "2008-04-09T06:50:58Z")

</div>

Simple solution: attachment\_fu

---

<div class="post-metadata">

**Author:** ![Aaaarg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9bcb4/32.png) [@Aaaarg](https://www.ruby-forum.com/u/Aaaarg)\
**Post date:** [April 9, 2008, 8:22am UTC](https://www.ruby-forum.com/t/simple-ror-problem-with-file-column-rmagick/126623/8 "2008-04-09T08:22:27Z")

</div>

Ar Chron wrote:

> Simple solution: attachment\_fu

Q: How can I tune my Mercedes?  
A: Buy a Porsche. 😉

---

<div class="post-metadata">

**Author:** ![Aaaarg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e9bcb4/32.png) [@Aaaarg](https://www.ruby-forum.com/u/Aaaarg)\
**Post date:** [April 10, 2008, 11:23pm UTC](https://www.ruby-forum.com/t/simple-ror-problem-with-file-column-rmagick/126623/9 "2008-04-10T23:23:48Z")

</div>

Sounds like a winner to me!

('course, I’ve always been partial to Porche’s… maybe cause I got to  
use one for HS prom… what a memory!)

As for the Rails bit, I think Ar’s right on. If you’ve already got  
rmagick working, just slap in attachment\_fu. It’s dirt-easy and quite  
powerful and configurable. If you are having to monkeypatch  
file\_column to access size data and the like, then I really, seriously  
recommend attachment\_fu and spend the rest of the otherwise-  
monkeypatch-time celebrating your fine Rails app over beers or lattes.

That’s my strategy, at any rate.

-Danimal

On Apr 9, 12:22 am, Peter S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
