I have an app where users upload images and tag them:
class Image < ActiveRecord::Base
has_many :taggings, :dependent => :destroy
has_many :tags, :through => :taggings
end
class Tag < ActiveRecord::Base
has_many :images, :through => :taggings
has_many :taggings, :dependent => :destroy
end
class Tagging < ActiveRecord::Base
belongs_to :image
belongs_to :tag
end
Before I was using HABTM for this kind of setup, but then I wanted to
have the status of the tag in the join table. In my app, tags are
disabled after a certain amount of time (I know it sounds strange). So
I read that has_many :through was what you’re supposed to use if you
want a join table with extra info in it.
I was hoping that by adding the table column “disabled” to the taggings
table I could do something like: post.tags[0].disabled, and get a
result, but I guess that’s not how AR operates.
So I figured I would write a method for the Tag model that would provide
the same functionality, so I could do post.tags[0].disabled? or
post.tags[0].enabled? and get the status of the tag in relation to the
calling post. I can’t figure out how to write such a method, though. I
can’t figure out how Tag object is supposed to know what Image object is
calling it; it seems to get lost in the intermediary table Tagging.
Any help would be appreciated. I’m baffled. Perhaps I am approaching
the entire situation wrong.
(Also, before anyone asks, I chose not to use acts_as_taggable because
a) I hate using prepackaged code, it makes my application feel cheap and
cookie-cutter-ish, b) it didn’t provide precisely the functions that I
needed and I didn’t feel like hacking it.)