# 3 newbie questions

**URL:** <https://www.ruby-forum.com/t/3-newbie-questions/98629>\
**Category:** Rails\
**Created:** [July 17, 2006, 2:53am UTC](https://www.ruby-forum.com/t/3-newbie-questions/98629 "2006-07-17T02:53:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [July 17, 2006, 2:53am UTC](https://www.ruby-forum.com/t/3-newbie-questions/98629/1 "2006-07-17T02:53:04Z")

</div>

hey all,  
I have 3 tables like this:  
Images (id,name)  
Tags (id,name)  
ImagesTags(imageid,tagid)

in my image model I do a “has\_and\_belongs\_to\_many :tags” but to make  
it work I need to rename ImagesTags(imageid,tagid) to  
Images\_Tags(image\_id,tag\_id). My question is that given that I can’t  
rename the table, is there any way to make it work with the original  
names?

Second question:  
I have 2 tables like this:  
Picture(id,name,albumid)  
Albums(id,name)

in my Picture model I put:  
belongs\_to :album, :foreign\_key =\> “dirid”

and in my Album model I put:  
has\_many :images

Yet, in my album/list.rhtml when I put :

\<% for picture in album.pictures %\> \<%= picture.name %\> \<% end %\>

I get:  
SQLite3::SQLException: no such column: pictures.album\_id: SELECT \*  
FROM pictures WHERE (pictures.album\_id = 1)

so it looks like the :foreign\_key =\> “dirid” is ignored. Any idea  
what I could make it work?

Third question:  
I would like to right some code that would work like this:  
\<% if thumbnail-file.jpg doesn’t exists execute create\_thumbnail()  
file.jpg %\>

I already have the create\_thumbnail() working. Any idea how to write  
this right?

thanx in advance

Pat

---

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [July 17, 2006, 3:23am UTC](https://www.ruby-forum.com/t/3-newbie-questions/98629/2 "2006-07-17T03:23:18Z")

</div>

Hi Pat!

On 17/07/2006, at 10:50 AM, Patrick A. wrote:

> names?

You don’t have to rename your table, make your habtm declaration look  
like this:

has\_and\_belongs\_to\_many :tags, :join\_table =\> ‘ImagesTags’

> so it looks like the :foreign\_key =\> “dirid” is ignored. Any idea  
> what I could make it work?

On your Album model you need to specify the foriegn\_key as well  
afaik, try:

has\_many :pictures, :foreign\_key =\> ‘dirid’

> Third question:  
> I would like to right some code that would work like this:  
> \<% if thumbnail-file.jpg doesn’t exists execute create\_thumbnail()  
> file.jpg %\>
> 
> I already have the create\_thumbnail() working. Any idea how to  
> write this right?

Could you explain this one a bit further?

Ryan.

---

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [July 18, 2006, 12:45am UTC](https://www.ruby-forum.com/t/3-newbie-questions/98629/3 "2006-07-18T00:45:27Z")

</div>

> > right?  
> > 1st check if /thumbnail/thumbi.jpg exists  
> > 2nd if it doesnÂ´t it should execute the function create\_thumbnail() file.jpg  
> > 3rd if it already exists do nothing
> 
> what I want to do is check if a file exists in a given directory and  
> if it doesnÂ´t then execute the function that creates it (that function  
> to create the file is already written).

\<% unless File.exist?(RAILS\_ROOT + “/thumbnail.thumbi.jpg”)  
create\_thumbnail()  
end  
%\>  
\<%= image\_tag(…) %\>

Although if you know the thumbnail you’re looking for before you get to  
your view (and I think you would) I’d do the creation in your  
controller.

Also, just something to keep in mind. What if two people hit that page  
at  
the exact same time and your create\_thumbnail() method gets called twice  
at the exact same time? Will the thumbnail get written out correctly?

-philip

---

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [July 18, 2006, 12:39am UTC](https://www.ruby-forum.com/t/3-newbie-questions/98629/4 "2006-07-18T00:39:08Z")

</div>

On 7/17/06, Ryan A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Third question:
> 
> I would like to right some code that would work like this:
> 
> \<% if thumbnail-file.jpg doesn’t exists execute create\_thumbnail() file.jpg  
> %\>

> I already have the create\_thumbnail() working. Any idea how to write this  
> right?

> Could you explain this one a bit further?
> 
> Ryan.  
> Thanx a lot for your answers Ryan. About the third question, I want to  
> display thumbnails that are in /thumbnails of images that are in my  
> /image. The thing is that if there is no thumbnail for a given image I  
> want to create it but if the thumnail exists it is not necessary to  
> recreate it.  
> so the code should:  
> 1st check if /thumbnail/thumbi.jpg exists  
> 2nd if it doesn´t it should execute the function create\_thumbnail()  
> file.jpg  
> 3rd if it already exists do nothing

what I want to do is check if a file exists in a given directory and  
if it doesn´t then execute the function that creates it (that function  
to create the file is already written).

thanx in advance

Pat

---

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [July 18, 2006, 1:07am UTC](https://www.ruby-forum.com/t/3-newbie-questions/98629/5 "2006-07-18T01:07:21Z")

</div>

On 7/18/06, Philip H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > right?  
> > > 1st check if /thumbnail/thumbi.jpg exists  
> > > %\>  
> > > \<%= image\_tag(…) %\>
> 
> Although if you know the thumbnail you’re looking for before you get to  
> your view (and I think you would) I’d do the creation in your controller.

> Also, just something to keep in mind. What if two people hit that page at  
> the exact same time and your create\_thumbnail() method gets called twice  
> at the exact same time? Will the thumbnail get written out correctly?

thanx for your answer. I guess that if two people access the site at  
the same time the first thumbnail will be overwritten by the second or  
no? is there a way to secure that kind of situation other than forcing  
the admin to generate thumbnails first?

---

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [July 20, 2006, 9:45pm UTC](https://www.ruby-forum.com/t/3-newbie-questions/98629/6 "2006-07-20T21:45:18Z")

</div>

On 7/16/06, Ryan A. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Images (id,name)  
> Images\_Tags(image\_id,tag\_id). My question is that given that I can’t  
> has\_and\_belongs\_to\_many :tags, :join\_table =\> ‘ImagesTags’

thanx but I still have to rename my column imageid to image\_id and tagid  
to  
tag\_id. Any idea how I could make it work with these column names?

thanx in advance

Pat

---

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [July 18, 2006, 1:29am UTC](https://www.ruby-forum.com/t/3-newbie-questions/98629/7 "2006-07-18T01:29:15Z")

</div>

> > > > right?  
> > > > 1st check if /thumbnail/thumbi.jpg exists  
> > > > end  
> > > > thanx for your answer. I guess that if two people access the site at  
> > > > the same time the first thumbnail will be overwritten by the second or  
> > > > no? is there a way to secure that kind of situation other than forcing  
> > > > the admin to generate thumbnails first?

Does an admin upload the images using a web form? If so, generate the  
thumbnail then. Otherwise, generate it and put it in a temporary file.  
Then move the file into place. That’s usually an atomic transaction so  
you won’t have to worry about a garbled file. You’ll still generate it  
twice though, but at least they won’t clobber each other and give you an  
unusuable file…

-philip
