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.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Could someone please help me with this? On Mar 23, 3:49 pm, "askme" <ChaitanyaPrakas...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 3/24/07, askme <ChaitanyaPrakash.N-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Could someone please help me with this?Have a look at "has_many :through", that should do what you need. --max> > On Mar 23, 3:49 pm, "askme" <ChaitanyaPrakas...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > 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. > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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! On Mar 24, 4:43 pm, "Max Muermann" <r...-DC/T6mWKptNg9hUCZPvPmw@public.gmane.org> wrote:> On 3/24/07, askme <ChaitanyaPrakas...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Could someone please help me with this? > > Have a look at "has_many :through", that should do what you need. > > --max > > > > > > > On Mar 23, 3:49 pm, "askme" <ChaitanyaPrakas...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > 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.- Hide quoted text - > > - Show quoted text ---~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---