Jim Jeffers
2006-Feb-10 05:39 UTC
[Rails] Handling a relationship between users and newsletter subcriptions.
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
Alex Young
2006-Feb-10 09:12 UTC
[Rails] Handling a relationship between users and newsletter subcriptions.
Jim Jeffers wrote:> 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?In Rails 1.0, you''re probably doing all you can, but this sounds like a perfect case for :through relationships in Edge Rails. -- Alex
Adam Fields
2006-Feb-10 14:22 UTC
[Rails] Handling a relationship between users and newsletter subcriptions.
On Thu, Feb 09, 2006 at 10:39:36PM -0700, Jim Jeffers wrote:> 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?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
Jim Jeffers
2006-Feb-11 04:21 UTC
[Rails] Re: Handling a relationship between users and newsletter sub
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-- Posted via http://www.ruby-forum.com/.
Adam Fields
2006-Feb-11 05:06 UTC
[Rails] Re: Handling a relationship between users and newsletter sub
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.> 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 ** 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