# Is this a use case for has\_many :through?

**URL:** <https://www.ruby-forum.com/t/is-this-a-use-case-for-has-many-through/169581>\
**Category:** Rails\
**Created:** [June 5, 2009, 5:06pm UTC](https://www.ruby-forum.com/t/is-this-a-use-case-for-has-many-through/169581 "2009-06-05T17:06:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![coolmenu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eada6e/32.png) [@coolmenu](https://www.ruby-forum.com/u/coolmenu)\
**Post date:** [June 5, 2009, 5:06pm UTC](https://www.ruby-forum.com/t/is-this-a-use-case-for-has-many-through/169581/1 "2009-06-05T17:06:30Z")

</div>

I’m building a blog application that allows users to create and edit  
blog posts. I want to add the ability for a user to check a box on  
the post page that enables an alert on the post that sends an email to  
the user when the post is updated.

I would think my alerts table would simply consist of a user\_id, a  
post\_id and a created\_at timestamp. So in modeling the associations  
for this, my first thought was that this would be a has\_many :through  
association but I’m not sure how or where I would make that  
association.

Is this thought process on the right path? Thanks for any help.

class Post \< ActiveRecord::Base  
belongs\_to :creator, :class\_name =\> “User”, :foreign\_key =\>  
“created\_by”  
has\_many :alerts, :dependent =\> :destroy  
end

class User \< ActiveRecord::Base  
has\_many :created\_posts, :class\_name =\> “Post”, :foreign\_key =\>  
“created\_by”  
has\_many :alerts, :dependent =\> :destroy  
end

class Alert \< ActiveRecord::Base  
belongs\_to :post  
belongs\_to :user  
end

---

<div class="post-metadata">

**Author:** ![coolmenu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eada6e/32.png) [@coolmenu](https://www.ruby-forum.com/u/coolmenu)\
**Post date:** [June 5, 2009, 5:18pm UTC](https://www.ruby-forum.com/t/is-this-a-use-case-for-has-many-through/169581/2 "2009-06-05T17:18:13Z")

</div>

basic question, Can a user update post of another user ?  
If yes  
then your approach is correct.

If no,  
then you need not have user\_id in alerts table  
in this case relationships will be

class Post \< ActiveRecord::Base  
belongs\_to :creator, :class\_name =\> “User”, :foreign\_key =\>  
“created\_by”  
has\_many :alerts, :dependent =\> :destroy  
end

class User \< ActiveRecord::Base  
has\_many :created\_posts, :class\_name =\> “Post”, :foreign\_key =\>  
“created\_by”  
has\_many :alerts, through =\> :created\_posts

# Need not have , :dependent =\> :destroy

end

class Alert \< ActiveRecord::Base  
belongs\_to :post  
end

-Sandip R~

On Fri, Jun 5, 2009 at 8:25 PM, JL Smith [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> association.  
> has\_many :created\_posts, :class\_name =\> “Post”, :foreign\_key =\>  
> “created\_by”  
> has\_many :alerts, :dependent =\> :destroy  
> end
> 
> class Alert \< ActiveRecord::Base  
> belongs\_to :post  
> belongs\_to :user  
> end
> 
> >

–  
Ruby on Rails Developer

> **[Blogs - Neuigkeiten - Artikel - Webseiten - Medien](https://www.sosblog.com/)**
>
> Auf SOSblog geht es um aktuelle Blogs, Neuigkeiten, Artikel, Webseiten und Medien. Wir bringen interessante neue Webseiten aus den Tiefen des Webs.

> **[Judul Situs](https://funonrails.wordpress.com/)**
>
> “Cinta tidak mengenal batas. Cinta mampu melewati rintangan, melompati pagar, menembus dinding untuk mencapai tujuannya dengan penuh harapan.” — Maya Angelou

> **[Josh Software | Custom Software Development Services & Solutions](https://joshsoftware.com/)**
>
> JoshSoftware offers custom software development Services & Solution that provides enterprise, software development services for web & mobile.

---

<div class="post-metadata">

**Author:** ![coolmenu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eada6e/32.png) [@coolmenu](https://www.ruby-forum.com/u/coolmenu)\
**Post date:** [June 5, 2009, 5:25pm UTC](https://www.ruby-forum.com/t/is-this-a-use-case-for-has-many-through/169581/3 "2009-06-05T17:25:17Z")

</div>

Yeah, another user can update somebody’s post but I don’t think that  
even matters in this case (think of this as having multiple admins  
editing posts). The alerts table still needs to connect both the user  
and the post so that a user other than the user that created the post  
can enable an alert on the post.

With that said, is this still a has\_many :through or will my original  
design work? Thanks again.

---

<div class="post-metadata">

**Author:** ![coolmenu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eada6e/32.png) [@coolmenu](https://www.ruby-forum.com/u/coolmenu)\
**Post date:** [June 5, 2009, 6:30pm UTC](https://www.ruby-forum.com/t/is-this-a-use-case-for-has-many-through/169581/4 "2009-06-05T18:30:23Z")

</div>

go ahead with your original design.

On Fri, Jun 5, 2009 at 8:54 PM, JL Smith [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 

–  
Ruby on Rails Developer

> **[Blogs - Neuigkeiten - Artikel - Webseiten - Medien](https://www.sosblog.com/)**
>
> Auf SOSblog geht es um aktuelle Blogs, Neuigkeiten, Artikel, Webseiten und Medien. Wir bringen interessante neue Webseiten aus den Tiefen des Webs.

> **[Judul Situs](https://funonrails.wordpress.com/)**
>
> “Cinta tidak mengenal batas. Cinta mampu melewati rintangan, melompati pagar, menembus dinding untuk mencapai tujuannya dengan penuh harapan.” — Maya Angelou

> **[Josh Software | Custom Software Development Services & Solutions](https://joshsoftware.com/)**
>
> JoshSoftware offers custom software development Services & Solution that provides enterprise, software development services for web & mobile.

---

<div class="post-metadata">

**Author:** ![coolmenu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/eada6e/32.png) [@coolmenu](https://www.ruby-forum.com/u/coolmenu)\
**Post date:** [June 5, 2009, 7:52pm UTC](https://www.ruby-forum.com/t/is-this-a-use-case-for-has-many-through/169581/5 "2009-06-05T19:52:40Z")

</div>

You’re on the right track - the :through part comes in when you want  
to go directly from Post to User. The following associations will do  
what you’re looking for:

- on Post  
has\_many :alert\_users, :through =\> :alerts, :source =\> :user

- on User  
has\_many :alert\_posts, :through =\> :alerts, :source =\> :post

(you may need some extra :class\_name noise, but I think this will  
work)

Then, given a post @post, you can get all the users receiving alerts:

@post.alert\_users

or all the posts a user @user is being alerted about:

@user.alert\_posts

–Matt J.
