# Confusion with has\_many :through

**URL:** <https://www.ruby-forum.com/t/confusion-with-has-many-through/87594>\
**Category:** Rails\
**Created:** [March 4, 2007, 5:02am UTC](https://www.ruby-forum.com/t/confusion-with-has-many-through/87594 "2007-03-04T05:02:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8491ac/32.png) [@anonymous](https://www.ruby-forum.com/u/anonymous)\
**Post date:** [March 4, 2007, 5:02am UTC](https://www.ruby-forum.com/t/confusion-with-has-many-through/87594/1 "2007-03-04T05:02:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anonymous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8491ac/32.png) [@anonymous](https://www.ruby-forum.com/u/anonymous)\
**Post date:** [March 4, 2007, 10:18am UTC](https://www.ruby-forum.com/t/confusion-with-has-many-through/87594/2 "2007-03-04T10:18:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![anonymous](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8491ac/32.png) [@anonymous](https://www.ruby-forum.com/u/anonymous)\
**Post date:** [March 5, 2007, 12:48am UTC](https://www.ruby-forum.com/t/confusion-with-has-many-through/87594/3 "2007-03-05T00:48:41Z")

</div>

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.
