# Attribute or instance method or what?

**URL:** <https://www.ruby-forum.com/t/attribute-or-instance-method-or-what/54547>\
**Category:** Rails\
**Created:** [March 8, 2006, 9:29am UTC](https://www.ruby-forum.com/t/attribute-or-instance-method-or-what/54547 "2006-03-08T09:29:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [March 8, 2006, 9:29am UTC](https://www.ruby-forum.com/t/attribute-or-instance-method-or-what/54547/1 "2006-03-08T09:29:39Z")

</div>

Hi!

Uhm are attributes just something like additional members of a class,  
other than those that come from the names of columns in the table?

I’ve got images table, which has ‘name’ column. I’ve added support for  
creating thumbnails out of uploaded pictures and i’d like to have an  
easy way of accessing them in my views.

How to do it? I.e. my image has name ‘1-13.jpg’ and thumbnail will have  
‘1-13-thumb.jpg’. Do i have to create additional attribute thumbnailname  
and somehow set it automatically based on ‘name’ value, so it will be  
available for an instance variable of my image class, or create a helper  
method for image class or…?

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [March 8, 2006, 10:22am UTC](https://www.ruby-forum.com/t/attribute-or-instance-method-or-what/54547/2 "2006-03-08T10:22:30Z")

</div>

CzeÅ?Ä? Szymek 🙂

On 08/03/06, szymek [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> How to do it? I.e. my image has name ‘1-13.jpg’ and thumbnail will have  
> ‘1-13-thumb.jpg’. Do i have to create additional attribute thumbnailname  
> and somehow set it automatically based on ‘name’ value, so it will be  
> available for an instance variable of my image class, or create a helper  
> method for image class or…?

The simpliest way would be to provide additional getter:

class Image  
def thumbnail\_name  
self.name.gsub(/.jpg$/, ‘-thumb.jpg’)  
end  
end

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [March 8, 2006, 10:31am UTC](https://www.ruby-forum.com/t/attribute-or-instance-method-or-what/54547/3 "2006-03-08T10:31:35Z")

</div>

On Mar 8, 2006, at 10:19, Åukasz Piestrzeniewicz wrote:

> > method for image class or…?
> 
> The simpliest way would be to provide additional getter:
> 
> class Image  
> def thumbnail\_name
> 
> end  
> end

And if you needed the thumbnail name in SQL land as well then write a  
custom name setter that delegates to AR this way:

class Image \< ActiveRecord::Base  
def name=(n)  
write\_attribute(:name, n)  
write\_attribute(:thumbnail\_name, n.gsub(/.jpg$/, ‘-thumb.jpg’))  
end  
end

– fxn

---

<div class="post-metadata">

**Author:** ![g0nzo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/3bc359/32.png) [@g0nzo](https://www.ruby-forum.com/u/g0nzo)\
**Post date:** [March 8, 2006, 11:05am UTC](https://www.ruby-forum.com/t/attribute-or-instance-method-or-what/54547/4 "2006-03-08T11:05:38Z")

</div>

Czesc!

Dzieki za odpowiedz!.. i mean, thanks for your answer 🙂

Previously i had name2thumbname as class method. Now i’ve changed  
everything to instance methods and it works great, but i’ve got one more  
question:

I’m adding new images to the database like this (in a loop):  
@property.images \<\< Image.new(:image\_url =\> image\_filename)

Is it possible to get id of new image without searching for it in the  
database? Maybe \<\< method returns new id?

Because since i’ve changed everything to instance methods now i add the  
image to the database and in the next line i search for it by the  
filename to get its instance. It works, but maybe there’s a better way.
