# Contextual Associations

**URL:** <https://www.ruby-forum.com/t/contextual-associations/105813>\
**Category:** Rails\
**Created:** [June 3, 2007, 9:14am UTC](https://www.ruby-forum.com/t/contextual-associations/105813 "2007-06-03T09:14:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [June 3, 2007, 9:14am UTC](https://www.ruby-forum.com/t/contextual-associations/105813/1 "2007-06-03T09:14:23Z")

</div>

No doubt this has been covered but I’ll be damned if I can find it.  
References appreciated so I can add to my library.

1. I have 3 tables in my problem: changes, environments, comments
2. changes are many-to-many to environments
3. changes has-many comments

Here’s the tricky part:

1. environments has-many comments dependent on the current  
environment.

Tables:  
changes, environments, changes\_environments, comments,  
changes\_comments, changes\_comments\_environments.

How in would one associate comments to an environment according to the  
change on which it’s called through?

# i.e.

change = Changes.find(params[:change\_id:])  
print “For change #{change.id}\n”  
change.environments.each do |env|  
print “-- For environment #{env.name}\n”  
env.comments.each do |c|  
“---- #{c.body}\n”  
end  
end

Your help and patience are deeply appreciated.

## Thanks,

Ryan

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [June 3, 2007, 9:33am UTC](https://www.ruby-forum.com/t/contextual-associations/105813/2 "2007-06-03T09:33:24Z")

</div>

> Here’s the tricky part:
> 
> 1. environments has-many comments dependent on the current  
> environment.

I messed that up. Too much typing. Here’s the correct text:

Here’s the tricky part:

1. environments has-many comments dependent on the current change into  
which the environment is loaded.

For example, the join table:  
create\_table :changes\_comments\_environments do |t|  
t.column :change\_id, :integer, :null =\> false  
t.column :comment\_id, :integer, :null =\> false  
t.column :environment\_id, :integer, :null =\> false  
end

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [June 6, 2007, 8:02pm UTC](https://www.ruby-forum.com/t/contextual-associations/105813/3 "2007-06-06T20:02:20Z")

</div>

I assume from the lack of response that this is easy and well-  
documented. I’ll keep looking.

---

<div class="post-metadata">

**Author:** ![ryan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a8b319/32.png) [@ryan](https://www.ruby-forum.com/u/ryan)\
**Post date:** [June 6, 2007, 8:44pm UTC](https://www.ruby-forum.com/t/contextual-associations/105813/4 "2007-06-06T20:44:32Z")

</div>

On 6/6/07, Ryan [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I assume from the lack of response that this is easy and well-  
> documented. I’ll keep looking.

No it’s neither. But an illustration of your planned schema would  
help.

It may be that you have a modelling problem or that there is a  
polymorphic aspect to your core models or your join tables.

In addition you don’t state whether you are using HABTM or  
has\_many :through for your many-many associations.

In any case I just figured out HABTM and HM :T 2 weeks ago,  
and this is at the edge of my ability.

There’s a blog out there called ‘has many through’ google for it  
or check the Ruby blogrolls. The guy in question is excellent  
when it comes to explaining or working around the bleeding  
edge of these kind of questions. You might get better answers  
there.
