Handling a relationship between users and newsletter subcrip


#1

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.”
www.DontTrustThisGuy.com
(480) 235-5201


#2

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.


#3

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.aquick.org/blog ] … Blog
[ http://www.adamfields.com/resume.html ]… Experience
[ http://www.flickr.com/photos/fields ] … Photos
[ http://www.aquicki.com/wiki ]…Wiki
[ http://del.icio.us/fields ] … Links


#4

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.aquick.org/blog ] … Blog
[ http://www.adamfields.com/resume.html ]… Experience
[ http://www.flickr.com/photos/fields ] … Photos
[ http://www.aquicki.com/wiki ]…Wiki
[ http://del.icio.us/fields ] … Links


#5

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