Forum: Ruby on Rails Handling a relationship between users and newsletter subcrip

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B614e42d18c609ac4585d0aa0b13d856?d=identicon&s=25 Jim Jeffers (Guest)
on 2006-02-10 06:39
(Received via mailing list)
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 Jeffers
"A trustworthy individual."
www.DontTrustThisGuy.com
(480) 235-5201
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (Guest)
on 2006-02-10 10:12
(Received via mailing list)
Jim Jeffers 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.
912436ffe7c27e25658a3f5f556a8c96?d=identicon&s=25 Adam Fields (Guest)
on 2006-02-10 15:23
(Received via mailing list)
On Thu, Feb 09, 2006 at 10:39:36PM -0700, Jim Jeffers 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
B614e42d18c609ac4585d0aa0b13d856?d=identicon&s=25 Jim Jeffers (Guest)
on 2006-02-11 05:21
Just as an example in that case how would I select all users that had a
subscription to newsletter 2?
- Jim

Adam Fields wrote:
> On Thu, Feb 09, 2006 at 10:39:36PM -0700, Jim Jeffers 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
912436ffe7c27e25658a3f5f556a8c96?d=identicon&s=25 Adam Fields (Guest)
on 2006-02-11 06:06
(Received via mailing list)
On Sat, Feb 11, 2006 at 05:21:05AM +0100, Jim Jeffers 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
This topic is locked and can not be replied to.