Is this a use case for has_many :through?

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 [email protected] 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

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 [email protected] wrote:


Ruby on Rails Developer

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 J.