# How do you model an article that has an author and readers w/ the same User model?

**URL:** <https://www.ruby-forum.com/t/how-do-you-model-an-article-that-has-an-author-and-readers-w-the-same-user-model/133913>\
**Category:** Rails\
**Created:** [April 4, 2008, 9:46pm UTC](https://www.ruby-forum.com/t/how-do-you-model-an-article-that-has-an-author-and-readers-w-the-same-user-model/133913 "2008-04-04T21:46:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![frank\_programmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/958977/32.png) [@frank\_programmer](https://www.ruby-forum.com/u/frank_programmer)\
**Post date:** [April 4, 2008, 9:46pm UTC](https://www.ruby-forum.com/t/how-do-you-model-an-article-that-has-an-author-and-readers-w-the-same-user-model/133913/1 "2008-04-04T21:46:29Z")

</div>

Hi,

I am stuck trying to figure out how to model this.

I have a user model and an article model.

It is easy enough to say the user has many articles and the article  
has one user.

class User \< ActiveRecord::Base  
has\_many :articles  
end

class Article \< ActiveRecord::Base  
belongs\_to :user  
end

But how do I add the concept of an article having many readers? The  
readers would be using the same User model since a User can author  
many articles but can also be reading many other articles.

Your help would be most appreciated.

Thanks,  
Frank

---

<div class="post-metadata">

**Author:** ![frank\_programmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/958977/32.png) [@frank\_programmer](https://www.ruby-forum.com/u/frank_programmer)\
**Post date:** [April 4, 2008, 11:56pm UTC](https://www.ruby-forum.com/t/how-do-you-model-an-article-that-has-an-author-and-readers-w-the-same-user-model/133913/2 "2008-04-04T23:56:56Z")

</div>

Well I’m certain this isn’t a working answer, I thought I’d post  
anyways in hopes that maybe something I say here will jump start your  
brain, and ultimately lead you to the solution. None of this code was  
tested.

#This one just uses habtm to make a join table for readers/reads

class User \< ActiveRecord::Base  
has\_many :articles  
has\_and\_belongs\_to\_many :reads, :class\_name =\> ‘Article’  
end

class Article \< ActiveRecord::Base  
belongs\_to :user  
has\_and\_belongs\_to\_many :readers, :class\_name =\> ‘User’  
end

* * *

# This one makes the join table explicitly and tries to use some fancy

has\_many options.

# The :source-'s might be flipped, I’m not sure how those work

exactly. Again this code

# isn’t tested, I just read through

> **[ActiveRecord::Associations::ClassMethods](https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html)**
>
> Associations are a set of macro-like class methods for tying objects together through foreign keys.

# and came up with the best thing I could.

class User \< ActiveRecord::Base  
has\_many :articles  
has\_many :readings, :through =\> :reads, :source =\> :user  
end

# Read as in ‘that was a good read(noun)’, NOT ‘he read(verb) well’.

Helps me to wrap my head around it better.  
class Read \< ActiveRecord::Base  
belongs\_to :user  
belongs\_to :article  
end

class Article \< ActiveRecord::Base  
belongs\_to :user  
has\_many :readers, :through =\> :reads, :source =\> :article  
end

Again I can’t stress enough that I do not know at all if this will  
work, so if your messing with it and you don’t possibly see how it  
could work, it probably doesn’t. Move on an find an answer from  
someone more experienced (I have less than 6 months exp. in Rails/  
Ruby, and use it much more as a hobby, and not a profession).

---

<div class="post-metadata">

**Author:** ![frank\_programmer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/958977/32.png) [@frank\_programmer](https://www.ruby-forum.com/u/frank_programmer)\
**Post date:** [April 5, 2008, 6:52pm UTC](https://www.ruby-forum.com/t/how-do-you-model-an-article-that-has-an-author-and-readers-w-the-same-user-model/133913/3 "2008-04-05T18:52:51Z")

</div>

I’d suggest a join class called ‘Subscription’ that belongs\_to :user  
and :article. With that you could also add some helpful attributes to  
the join (e.g., ‘can\_reply’, or ‘last\_read\_at’). If you add that then

User  
has\_many :articles  
has\_many :subscriptions

has\_many :articles\_to\_read, :class\_name=\>‘Articles’,  
:through=\>subscriptions  
end

Article  
belongs\_to :user  
has\_many :subscriptions  
has\_many :readers, :class\_name=\>‘User’, :through=\>:subscriptions  
end
