# Linked associations question please help

**URL:** <https://www.ruby-forum.com/t/linked-associations-question-please-help/177498>\
**Category:** Rails\
**Created:** [October 19, 2009, 9:47am UTC](https://www.ruby-forum.com/t/linked-associations-question-please-help/177498 "2009-10-19T09:47:00Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![theambler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7cd45c/32.png) [@theambler](https://www.ruby-forum.com/u/theambler)\
**Post date:** [October 19, 2009, 9:47am UTC](https://www.ruby-forum.com/t/linked-associations-question-please-help/177498/1 "2009-10-19T09:47:00Z")

</div>

I have a Users model. Users ‘have many’ Stories, and Stories have  
many Items.

This structure allows the sharing of Items between Stories and Stories  
between Users.

But given there is no direct ‘ownership’ link between Users and Items,  
what is the best way of building a list of Items which belong to a  
Story to which a given User has access.

something like:

current\_user.stories.items

obviously doesn’t work.

I’ve written some nested blocks ie:

users.each do |user|  
user.stories.each.do |story|  
story.items.each.do |item|  
end  
end  
end

which works but is ugly and slow.

I could keep an index table of items linked to users but this is  
redundant for most of my application. I’m sure there is a more  
obvious, simple way of doing this but I haven’t come across it.

Any help gratefully received.

---

<div class="post-metadata">

**Author:** ![theambler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7cd45c/32.png) [@theambler](https://www.ruby-forum.com/u/theambler)\
**Post date:** [October 19, 2009, 9:59am UTC](https://www.ruby-forum.com/t/linked-associations-question-please-help/177498/2 "2009-10-19T09:59:36Z")

</div>

Hi

```
You have to look at 'nested has_many associations'. Search it here

```

> **[Active Record Associations — Ruby on Rails Guides](https://guides.rubyonrails.org/association_basics.html)**
>
> Active Record AssociationsThis guide covers the association features of Active Record.After reading this guide, you will know: How to declare associations between Active Record models. How to understand the various types of Active Record...

Sijo

---

<div class="post-metadata">

**Author:** ![theambler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7cd45c/32.png) [@theambler](https://www.ruby-forum.com/u/theambler)\
**Post date:** [October 19, 2009, 10:20am UTC](https://www.ruby-forum.com/t/linked-associations-question-please-help/177498/3 "2009-10-19T10:20:34Z")

</div>

And By applying this in above case you can directly say

current\_user.items

Sijo

---

<div class="post-metadata">

**Author:** ![theambler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7cd45c/32.png) [@theambler](https://www.ruby-forum.com/u/theambler)\
**Post date:** [October 19, 2009, 11:58am UTC](https://www.ruby-forum.com/t/linked-associations-question-please-help/177498/4 "2009-10-19T11:58:34Z")

</div>

Hi

Have you anywhere specified the HABTM relation?It not supported I  
think. Please check your relations like

user.rb  
user has\_many stories  
user has\_many items through stories

story.rb  
belongs\_to user  
has\_many items

item.rb  
belongs\_to story

Sijo

---

<div class="post-metadata">

**Author:** ![theambler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7cd45c/32.png) [@theambler](https://www.ruby-forum.com/u/theambler)\
**Post date:** [October 19, 2009, 3:55pm UTC](https://www.ruby-forum.com/t/linked-associations-question-please-help/177498/5 "2009-10-19T15:55:14Z")

</div>

Users have many stories through userstorylinks…

has\_many :userstorylinks  
has\_many :stories, :through=\>:userstorylinks

Stories are even more associated…

has\_many :userstorylinks, :dependent=\>:destroy  
has\_many :users, :through=\>:userstorylinks  
has\_many :storyitemlinks  
has\_many :items, :through=\>:storyitemlinks  
has\_many :storycontactlinks  
has\_many :contacts, :through=\>:storycontactlinks  
has\_many :storycompanylinks  
has\_many :companies, :through=\>:storycompanylinks

Does this screw up your suggestion?

On Oct 19, 10:58 am, Sijo k g [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![theambler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/7cd45c/32.png) [@theambler](https://www.ruby-forum.com/u/theambler)\
**Post date:** [October 19, 2009, 11:44am UTC](https://www.ruby-forum.com/t/linked-associations-question-please-help/177498/6 "2009-10-19T11:44:40Z")

</div>

Thanks so much for the quick response.

Yes that makes sense, and I wasn’t aware it was possible. However  
having added to my User model:

has\_many :items, :through=\>:stories

I am getting the following error when trying to call @user.items

ActiveRecord::HasManyThroughSourceAssociationMacroError: Invalid  
source reflection macro :has\_many :through for  
has\_many :items, :through =\> :stories. Use :source to specify the  
source reflection.

I have to admit Reflections are something beyond my knowledge, and I  
noted suggestions that nested HABTM associations weren’t supported in  
earlier versions of Rails.

Any thoughts?
