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?
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?
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?
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.
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.
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!
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
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.
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.
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.
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