Forum: Ruby on Rails Is this a use case for has_many :through?

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.
3ff9c2544554b13c6c7877e76684751b?d=identicon&s=25 Lee Smith (smitjel)
on 2009-06-05 17:06
(Received via mailing list)
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
4ab2692ba129cb296d4f2e90ba5241da?d=identicon&s=25 Sandip Ransing (san2821)
on 2009-06-05 17:18
(Received via mailing list)
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@gmail.com> wrote:

> association.
>  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
3ff9c2544554b13c6c7877e76684751b?d=identicon&s=25 Lee Smith (smitjel)
on 2009-06-05 17:25
(Received via mailing list)
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.
4ab2692ba129cb296d4f2e90ba5241da?d=identicon&s=25 Sandip Ransing (san2821)
on 2009-06-05 18:30
(Received via mailing list)
go ahead with your original design.

On Fri, Jun 5, 2009 at 8:54 PM, JL Smith <autiger02@gmail.com> wrote:

>
--
Ruby on Rails Developer
http://sandip.sosblog.com
http://funonrails.wordpress.com
www.joshsoftware.com
6883e5ef03484d4fcef507d7b4f1d243?d=identicon&s=25 Matt Jones (Guest)
on 2009-06-05 19:52
(Received via mailing list)
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
This topic is locked and can not be replied to.