Forum: Ruby on Rails Help me! ... Problem with associations,

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
askme (Guest)
on 2007-03-23 12:50
(Received via mailing list)
Hi there,

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

  release (id)
  branch (id, release_id)
  platform (id)

   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
          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

 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 =
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 =
branches_platforms.platform_id '+
branches_platforms.branch_id '+
                                      'IN (SELECT '+
                                      '    FROM branches '+
                                      '    WHERE branches.release_id=
Thanks in advance.
askme (Guest)
on 2007-03-24 05:13
(Received via mailing list)
Could someone please help me with this?
Max M. (Guest)
on 2007-09-26 01:02
(Received via mailing list)
On 3/24/07, askme <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.

askme (Guest)
on 2007-09-26 01:03
(Received via mailing list)

 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

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

  I'm stuck here!
This topic is locked and can not be replied to.