# Help me! ... Problem with associations,

**URL:** <https://www.ruby-forum.com/t/help-me-problem-with-associations/89859>\
**Category:** Rails\
**Created:** [March 23, 2007, 11:50am UTC](https://www.ruby-forum.com/t/help-me-problem-with-associations/89859 "2007-03-23T11:50:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![askme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d6d6ee/32.png) [@askme](https://www.ruby-forum.com/u/askme)\
**Post date:** [March 23, 2007, 11:50am UTC](https://www.ruby-forum.com/t/help-me-problem-with-associations/89859/1 "2007-03-23T11:50:16Z")

</div>

Hi there,

I’ve three tables: release, branch and platform defined as follows:

release (id)  
branch (id, release\_id)  
platform (id)

## Associations:

1. Each branch belongs to zero or more platforms
2. Each platform has zero or more branches  
==\> To maintain this association, I’ve created the ‘join’ table as  
follows:  
branches\_platforms(branch\_id, platform\_id)  
and in the models,  
Branch: has\_and\_belongs\_to :platforms  
Platform: has\_and\_belongs\_to :branches
3. Each branch belongs to exactly one release
4. Each release has zero or more branches  
==\> Release: has\_many :branches  
Branch : belongs\_to :release

Now, if I would like to know the releases a platform supports or the  
platforms a release is applicable to, how do I define the  
association.

For time-being, I’m using :finder\_sql to specificy the complete SQL  
query, as shown below. Is there an easy way to specify such an  
association? Your help would be very invaluable.

Platform: has\_many :releases, :finder\_sql =\> 'SELECT releases.\* '+  
'FROM releases '+  
'INNER JOIN branches '+  
'ON releases.id =  
branches.release\_id '+  
'WHERE branches.branch\_id '+  
'IN (SELECT  
branches\_platforms.branch\_id '+  
’ FROM branches\_platforms ‘+  
’ WHERE  
branches\_platforms.platform\_id= #{id}’

Release: has\_many :platforms, :finder\_sql =\> 'SELECT platforms.\* '+  
'FROM platforms '+  
'INNER JOIN branches\_platforms  
'+  
'ON platforms.id =  
branches\_platforms.platform\_id '+  
'WHERE  
branches\_platforms.branch\_id '+  
'IN (SELECT branches.id '+  
’ FROM branches ‘+  
’ WHERE branches.release\_id=  
#{id})’  
Thanks in advance.

---

<div class="post-metadata">

**Author:** ![askme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d6d6ee/32.png) [@askme](https://www.ruby-forum.com/u/askme)\
**Post date:** [March 24, 2007, 4:13am UTC](https://www.ruby-forum.com/t/help-me-problem-with-associations/89859/2 "2007-03-24T04:13:15Z")

</div>

Could someone please help me with this?

---

<div class="post-metadata">

**Author:** ![askme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d6d6ee/32.png) [@askme](https://www.ruby-forum.com/u/askme)\
**Post date:** [September 25, 2007, 11:02pm UTC](https://www.ruby-forum.com/t/help-me-problem-with-associations/89859/3 "2007-09-25T23:02:40Z")

</div>

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

> Could someone please help me with this?

Have a look at “has\_many :through”, that should do what you need.

–max

---

<div class="post-metadata">

**Author:** ![askme](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d6d6ee/32.png) [@askme](https://www.ruby-forum.com/u/askme)\
**Post date:** [September 25, 2007, 11:03pm UTC](https://www.ruby-forum.com/t/help-me-problem-with-associations/89859/4 "2007-09-25T23:03:30Z")

</div>

Max,

thank you very much. I’ve tried the following,

In release.rb (model),

has\_many :platforms, :through =\> :branches

this results in an error, which asks to specify, :source also (I  
doubt this is because platform\_id is not present in the branches table  
directly!)…

neither :source =\> release nor :source =\> platforms works.

I’m stuck here!
