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
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 <autiger02-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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 > > >-- Ruby on Rails Developer http://sandip.sosblog.com http://funonrails.wordpress.com www.joshsoftware.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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.
go ahead with your original design. On Fri, Jun 5, 2009 at 8:54 PM, JL Smith <autiger02-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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. > > >-- Ruby on Rails Developer http://sandip.sosblog.com http://funonrails.wordpress.com www.joshsoftware.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Jones On Jun 5, 10:55 am, JL Smith <autige...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Is this thought process on the right path? Thanks for any help. >