Confusion with has_many :through

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.)

Anonymous wrote:

end
want a join table with extra info in it.
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.)

It works for me:

image = Image.find(1)
=> #<Image:0x2aaaad5133d0 @attributes={“name”=>“image1.jpg”, “id”=>“1”}>

image.tags
=> [#<Tag:0x2aaaad50eab0 @attributes={“name”=>“panda”, “id”=>“1”}>,
#<Tag:0x2aaaad50e9c0 @attributes={“name”=>“forest”, “id”=>“3”}>]

image.taggings
=> [#<Tagging:0x2aaaad509948 @attributes={“tag_id”=>“1”, “id”=>“1”,
“disabled”=>“0”, “image_id”=>“1”}>, #<Tagging:0x2aaaad509920
@attributes={“tag_id”=>“3”, “id”=>“3”, “disabled”=>“1”,
“image_id”=>“1”}>]

image.taggings[0].disabled
=> 0

image.taggings[1].disabled
=> 1


Michael W.

I think there was a reason why taggings[0].disabled was not as
functional as tags[0].disabled in my situation but I can no longer
remember what that reason was. Maybe there isn’t.

I guess for now image.taggings[0].disabled works instead of
image.tags[0].disabled, although a part of me thinks that
image.tags[0].disabled is more sensible way to do it. From a functional
standpoint it makes no difference.

I might have figured this out for myself if I wasn’t so often stubborn
about how I want to solve a problem and how I think it’s meant to be
solved.