# Through Associations

**URL:** <https://www.ruby-forum.com/t/through-associations/110472>\
**Category:** Rails\
**Created:** [July 26, 2007, 5:05pm UTC](https://www.ruby-forum.com/t/through-associations/110472 "2007-07-26T17:05:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Farhan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/b2d939/32.png) [@Farhan](https://www.ruby-forum.com/u/Farhan)\
**Post date:** [July 26, 2007, 5:05pm UTC](https://www.ruby-forum.com/t/through-associations/110472/1 "2007-07-26T17:05:53Z")

</div>

Hi,

I’m a total noob to rails. I’m working my way through my first app and  
I desperately need help with through associations in rails.

Task: get number of unread articles.

Database structure:  
A. Users  
has\_many :activity  
has\_many :article, :through =\> :activity

B. Activity  
belongs\_to :user  
belongs\_to :article

C. Articles (user\_id,feed\_id,is\_read)  
has\_many :reading  
has\_many :user, :through =\> :reading  
belongs\_to :feed

Any article that is in this table has been read and the “is\_read”  
field has been set to true.

D. Feeds  
has\_many :article

How do I go about getting the number of unread articles for a given  
user and feed?

Any help will be greatly appreciated.

Thanks in advance.

Best,  
Farhan

Sent from my iPhone

---

<div class="post-metadata">

**Author:** ![Farhan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/b2d939/32.png) [@Farhan](https://www.ruby-forum.com/u/Farhan)\
**Post date:** [July 26, 2007, 6:35pm UTC](https://www.ruby-forum.com/t/through-associations/110472/2 "2007-07-26T18:35:01Z")

</div>

Farhan wrote:

> Hi,
> 
> I’m a total noob to rails. I’m working my way through my first app and  
> I desperately need help with through associations in rails.

Through associations can be a bit complex for a beginner. Are you sure  
you don’t want to start with something simpler? Have you worked through  
the tutorial in AWDWR yet?

> Task: get number of unread articles.
> 
> Database structure:  
> A. Users  
> has\_many :activity  
> has\_many :article, :through =\> :activity

Well, for starters, you should always use plurals with has\_many  
relationships. So, “has\_many :activities, has\_many :articles, :through  
=\> :activities”, etc. AWDWR would have taught you this, and many other  
fundamental concepts.

> B. Activity  
> belongs\_to :user  
> belongs\_to :article

good. But I’m not sure I understand the purpose of this model. What’s  
an “activity” as it relates to a user and an article?

> C. Articles (user\_id,feed\_id,is\_read)  
> has\_many :reading  
> has\_many :user, :through =\> :reading  
> belongs\_to :feed

Again, “:has\_many :readings”

> Any article that is in this table has been read and the “is\_read”  
> field has been set to true.

What table? Articles? I think you forgot to post a description of  
your “Reading” model.

> D. Feeds  
> has\_many :article

:articles

> How do I go about getting the number of unread articles for a given  
> user and feed?

Not sure I can really help here without a better understanding of the  
purpose of the Activity model and the structure of your Reading model.

> Any help will be greatly appreciated.
> 
> Thanks in advance.
> 
> Best,  
> Farhan  
> –  
> [http://www.5valleys.com/](http://www.5valleys.com/)

---

<div class="post-metadata">

**Author:** ![Farhan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/b2d939/32.png) [@Farhan](https://www.ruby-forum.com/u/Farhan)\
**Post date:** [July 26, 2007, 7:03pm UTC](https://www.ruby-forum.com/t/through-associations/110472/3 "2007-07-26T19:03:04Z")

</div>

Hi John,

Thanks for replying. I’m will be getting the AWDWR book this evening.

Okay; so I made the changes to pluralize the associations for  
has\_many. Thanks for the tip; makes the code more readable.

I actually made a couple of typos in the “Database Structure”. I had  
changed the name of the “readings” to “activity” (now “activities”).  
The new database structure is as follows with the changes you  
requested:

Database structure:  
A. Users  
has\_many :activities  
has\_many :articles, :through =\> :activities

B. Activity (user\_id,feed\_id,is\_read)  
belongs\_to :user  
belongs\_to :article

C. Articles  
has\_many :activities  
has\_many :users, :through =\> :activities  
belongs\_to :feed

D. Feeds  
has\_many :articles

The activity model will keep track of all read articles for the user.  
That is when a user reads an article it gets added to the “activities”  
table. This table has the user\_id, article\_id, and the is\_read  
columns. Any article that is added to the activity model has the  
is\_read column set to “true”.

Hope this helps; otherwise let me know and I will provide more  
information.

Thanks,  
Farhan

---

<div class="post-metadata">

**Author:** ![Farhan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/b2d939/32.png) [@Farhan](https://www.ruby-forum.com/u/Farhan)\
**Post date:** [July 26, 2007, 8:01pm UTC](https://www.ruby-forum.com/t/through-associations/110472/4 "2007-07-26T20:01:10Z")

</div>

John,

Thank you. I will try this two liner solution. Yes, in B the activity  
table has the “article\_id” and not “feed\_id”, sorry.

Thank you for your patience,  
Farhan

---

<div class="post-metadata">

**Author:** ![Farhan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/b2d939/32.png) [@Farhan](https://www.ruby-forum.com/u/Farhan)\
**Post date:** [July 26, 2007, 7:41pm UTC](https://www.ruby-forum.com/t/through-associations/110472/5 "2007-07-26T19:41:08Z")

</div>

insha wrote:

> requested:  
> C. Articles  
> columns. Any article that is added to the activity model has the  
> is\_read column set to “true”.
> 
> Hope this helps; otherwise let me know and I will provide more  
> information.
> 
> Thanks,  
> Farhan

Alright, first… at B. above, you say the activity table has a  
“feed\_id” column, but below you say it’s “article\_id”. I 'm going to  
plow forward with the second and ignore the first, since that what it  
should be.

Ok, so now we want to get all the _read_ articles for a particular user.

Add something like this to your User class.  
has\_many :readings, :class\_name =\> ‘Activity’, :conditions =\> ‘is\_read =  
1’  
has\_many :read\_articles, :through =\> :readings

I hope that helps.

–  
[http://www.5valleys.com/](http://www.5valleys.com/)

---

<div class="post-metadata">

**Author:** ![Farhan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/b2d939/32.png) [@Farhan](https://www.ruby-forum.com/u/Farhan)\
**Post date:** [July 26, 2007, 10:09pm UTC](https://www.ruby-forum.com/t/through-associations/110472/6 "2007-07-26T22:09:24Z")

</div>

Here’s another scenario, since I’m getting confused:

I have the following Models:

A = User (name)  
B = Feed (name)  
C = Article (feed\_id, title)  
D = Reading (user\_id, article\_id, is\_read)

What would be the simplest/easiest way to associate, using :through  
associations, these models so that I can easily do the following:

1. Get all articles that a user _has_ read
2. Get all articles that a user has _not_ read

Best,  
Farhan
