# Handling a relationship between users and newsletter subcrip

**URL:** <https://www.ruby-forum.com/t/handling-a-relationship-between-users-and-newsletter-subcrip/65251>\
**Category:** Rails\
**Created:** [February 10, 2006, 6:39am UTC](https://www.ruby-forum.com/t/handling-a-relationship-between-users-and-newsletter-subcrip/65251 "2006-02-10T06:39:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![aantix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f0a364/32.png) [@aantix](https://www.ruby-forum.com/u/aantix)\
**Post date:** [February 10, 2006, 6:39am UTC](https://www.ruby-forum.com/t/handling-a-relationship-between-users-and-newsletter-subcrip/65251/1 "2006-02-10T06:39:42Z")

</div>

Hey Everyone!

I have a newsletter system that contains a multitude of different  
newsletters. In this case three but the system allows the user to  
add more. Every user can be subscribed to any amount of given  
newsletters. So what I did was I created a user model, newsletter  
model, and subscription model.

The subscription model belongs to one user and one newsletter.  
However, I’m not quite sure what to do after that. Is this the  
proper way to perform this type of relationship or is it something  
where I would give the newsletter model a has\_and\_belongs\_to\_many  
users relationship? I’m not quite sure how to go about it. Any advice?

Thanks!

* * *

Jim J.  
“A trustworthy individual.”

> **[The personal blog of Jim Jeffers | DontTrustThisGuy.com](http://www.donttrustthisguy.com)**
>
> The personal blog of Jim Jeffers.

(480) 235-5201

---

<div class="post-metadata">

**Author:** ![aantix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f0a364/32.png) [@aantix](https://www.ruby-forum.com/u/aantix)\
**Post date:** [February 10, 2006, 10:12am UTC](https://www.ruby-forum.com/t/handling-a-relationship-between-users-and-newsletter-subcrip/65251/2 "2006-02-10T10:12:57Z")

</div>

Jim J. wrote:

> way to perform this type of relationship or is it something where I  
> would give the newsletter model a has\_and\_belongs\_to\_many users  
> relationship? I’m not quite sure how to go about it. Any advice?  
> In Rails 1.0, you’re probably doing all you can, but this sounds like a  
> perfect case for :through relationships in Edge Rails.

---

<div class="post-metadata">

**Author:** ![aantix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f0a364/32.png) [@aantix](https://www.ruby-forum.com/u/aantix)\
**Post date:** [February 10, 2006, 3:23pm UTC](https://www.ruby-forum.com/t/handling-a-relationship-between-users-and-newsletter-subcrip/65251/3 "2006-02-10T15:23:14Z")

</div>

On Thu, Feb 09, 2006 at 10:39:36PM -0700, Jim J. wrote:

> proper way to perform this type of relationship or is it something  
> where I would give the newsletter model a has\_and\_belongs\_to\_many  
> users relationship? I’m not quite sure how to go about it. Any advice?

Because the subscription itself likely has other properties  
(expiration date, for example), you don’t want to model that as a  
HABTM.

I think you probably want to do user has\_many (or has\_one)  
subscriptions, and subscriptions has\_many\_and\_belongs\_to newsletters.

–  
- Adam

\*\* Expert Technical Project and Business Management  
\*\*\*\* System Performance Analysis and Architecture  
\*\*\*\*\*\* [[http://www.everylastounce.com](http://www.everylastounce.com) ]

[[Adam Fields (weblog) - - entertaining hundreds of millions of eyeball atoms every day](http://www.aquick.org/blog) ] … Blog  
[[Adam Fields Resume](http://www.adamfields.com/resume.html) ]… Experience  
[[Adam Fields | Flickr](http://www.flickr.com/photos/fields) ] … Photos  
[[http://www.aquicki.com/wiki](http://www.aquicki.com/wiki) ]…Wiki  
[[http://del.icio.us/fields](http://del.icio.us/fields) ] … Links

---

<div class="post-metadata">

**Author:** ![aantix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f0a364/32.png) [@aantix](https://www.ruby-forum.com/u/aantix)\
**Post date:** [February 11, 2006, 6:06am UTC](https://www.ruby-forum.com/t/handling-a-relationship-between-users-and-newsletter-subcrip/65251/4 "2006-02-11T06:06:50Z")

</div>

On Sat, Feb 11, 2006 at 05:21:05AM +0100, Jim J. wrote:

> Just as an example in that case how would I select all users that had a  
> subscription to newsletter 2?

You’d have to also add subscription belongs\_to users.

Also, I mistyped - it’s has\_and\_belongs\_to\_many.

In this case, you’d do something like:

users = Array.new  
newsletter = Newsletter.find(2)  
newsletter.subscriptions.each { |subscription| users \<\<  
subscription.user }

I haven’t tried this, but I think that’s right. There may be an easier  
way.

> > I think you probably want to do user has\_many (or has\_one)  
> > subscriptions, and subscriptions has\_many\_and\_belongs\_to newsletters.

–  
- Adam

\*\* Expert Technical Project and Business Management  
\*\*\*\* System Performance Analysis and Architecture  
\*\*\*\*\*\* [[http://www.everylastounce.com](http://www.everylastounce.com) ]

[[Adam Fields (weblog) - - entertaining hundreds of millions of eyeball atoms every day](http://www.aquick.org/blog) ] … Blog  
[[Adam Fields Resume](http://www.adamfields.com/resume.html) ]… Experience  
[[Adam Fields | Flickr](http://www.flickr.com/photos/fields) ] … Photos  
[[http://www.aquicki.com/wiki](http://www.aquicki.com/wiki) ]…Wiki  
[[http://del.icio.us/fields](http://del.icio.us/fields) ] … Links

---

<div class="post-metadata">

**Author:** ![aantix](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f0a364/32.png) [@aantix](https://www.ruby-forum.com/u/aantix)\
**Post date:** [February 11, 2006, 5:21am UTC](https://www.ruby-forum.com/t/handling-a-relationship-between-users-and-newsletter-subcrip/65251/5 "2006-02-11T05:21:05Z")

</div>

Just as an example in that case how would I select all users that had a  
subscription to newsletter 2?

- Jim

Adam F. wrote:

> On Thu, Feb 09, 2006 at 10:39:36PM -0700, Jim J. wrote:
> 
> > proper way to perform this type of relationship or is it something  
> > where I would give the newsletter model a has\_and\_belongs\_to\_many  
> > users relationship? I’m not quite sure how to go about it. Any advice?
> 
> Because the subscription itself likely has other properties  
> (expiration date, for example), you don’t want to model that as a  
> HABTM.
> 
> I think you probably want to do user has\_many (or has\_one)  
> subscriptions, and subscriptions has\_many\_and\_belongs\_to newsletters.
> 
> –  
> - Adam
