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 : <td><% for picture in album.pictures %> <%= picture.name %> <% end %> </td> 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
Hi Pat! On 17/07/2006, at 10:50 AM, Patrick Aljord wrote:> 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? >You don''t have to rename your table, make your habtm declaration look like this: has_and_belongs_to_many :tags, :join_table => ''ImagesTags''> 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 : > <td><% for picture in album.pictures %> > <%= picture.name %> > <% end %> > </td> > > 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? >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. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060717/73087e92/attachment.html
On 7/17/06, Ryan Allen <ryan@badfont.com> 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
> On 7/17/06, Ryan Allen <ryan@badfont.com> 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).<% 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
On 7/18/06, Philip Hallstrom <rails@philip.pjkh.com> wrote:> > On 7/17/06, Ryan Allen <ryan@badfont.com> 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). > > <% 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? >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?
>> >> 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). >> >> <% 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? >> > 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
On 7/16/06, Ryan Allen <ryan@badfont.com> wrote:> > > Hi Pat! > > On 17/07/2006, at 10:50 AM, Patrick Aljord wrote: > > 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? > > > > You don''t have to rename your table, make your habtm declaration look like > this: > > 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 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060720/071279c0/attachment.html