Tagging pictures

I'm working on an app which has an image upload facility for users, I
want users to be able to create 'albums' for their pictures.
The way I'm thinking about implementing this is to have a hash in the
user model for each user which they can add categories to and will then
be available as a dropdown for any images they are uploading. This will
allow them to attach the category to the image. I can then filter the
images by category to show the albums. Does this seem like a sensible
way to do this? I don't want to use the acts_as_taggable_on gem as it
seems like overkill for what I'm doing (although maybe not - open to
suggestion..).

So, my question is, does my solution seem like the way to go?

Is there a better solution?

I did something similar to this and acts_as_taggable was a pretty lean solution. What makes you not want to use it?

-James

I've nothing specifically against it, but as I say I think just adding a
hash to the user model and then filtering on the entries therein seems
like it would be a lot more light weight no?

Is there some specific advantage I'd get by using acts_as_taggable_on?

Well if you are building a hash on the user model then you are sort of constrained by your tags/categories. With acts_as_taggable it is much easier for users (or you) to tag comment with keywords. This negates the need to maintain the hash in the user model.

-James

OK, I see what you mean, however, I'd like each user to have their own
categories only and they would likely only have 5 - 10 max for the most
part, as they would only be able to put each photo in one category so
the album analogy would work. So in that sense they are not really tags
like a taxonomy if you get my thinking? So I still think the taggable
gems would maybe be better suited in the case of having a taxonomy where
multiple tags could be used across the site - not just per user, more
like hashtags on tweets or the like?

I see where you’re going with this and now that I understand your use case I think acts_as_taggable isn’t what you want. You can build a hash with categories that can be manipulated by the user, or you can create a model/association between user/categories/images that would probably fit your use case better.

Interesting, do you mean this sort of thing:

http://guides.rubyonrails.org/association_basics.html

would you do that like-

has many through?

I'll take a look at that..

Interesting, do you mean this sort of thing:

http://guides.rubyonrails.org/association_basics.html

would you do that like-

has many through?

Possibly something like
User has_many albums

Album belongs_to user
Album has_many images

Image belongs_to album

If you want to be able to reference all the images belonging to a user
at once (whatever album they are in) then possibly also
User has_many images through albums

Colin

hmmm...

OK - I was thinking:

User has_many Albums
User has_many Images through Albums

Image belongs_to User
Image belongs_to Album

Album has_one user
Album has_many images

am I overcomplicating things here? I have to say I'm not sure what the
advantages/disadvantages to the setup above would be vs the one you
suggest Colin?

Might need to do some research, complicated stuff!

Have you thought about a possible polymorphic association between user, albums, and images? It may be a more straightforward approach for you.

You can read about it in the Rails association guide or if you have specific questions I’m sure one of us can help!

-James

OK,

thanks for the pointers - I'll get through a bit of reading on this
tomorrow and see if I can't make enough sense of it to get something
working..

Thanks again,

J.

hmmm...

OK - I was thinking:

User has_many Albums
User has_many Images through Albums

Image belongs_to User
Image belongs_to Album

Album has_one user
Album has_many images

am I overcomplicating things here? I have to say I'm not sure what the
advantages/disadvantages to the setup above would be vs the one you
suggest Colin?

The disadvantage of yours is that it is wrong. With user has_many
images through albums you should not have image belongs_to user. Also
with user has_many albums you should have album belongs_to user.

I sense that you are a beginner with rails, and if so suggest that you
first work right through the tutorial railstutorial.org (which is free
to use online) which will show you the basics of rails.

Colin

Yes - I see that, it was late, got albums and images the wrong way round
there..

I've been learning for a few months now, so I've seen and completed that
tutorial and a few others. Its an excellent resource especially
considering its free!

I'm going to do a bit of reading up on associations today, but think i
may yet just go with a hash or array of album names for simplicity,
unless something major jumps out at me.

Thanks again for the tips,

J.

Don't do that, you will regret it. Associations and the rails magic
they give you are one of the fundamentals of rails. For example,
having setup the associations you will be able to get the albums for a
user using

current_user.albums

and to get the images for a user for a particular album something like
current_user.albums.where( category: "the category").images

The code you will need to write to do the same using a hash will be
much more complex

Colin

OK,

thanks Colin, I will most likely go that route then,

I dare say it will save pain later on should I want to do anything more
complex with this as well,

J.