Jump to content

The ultimate community for Ruby on Rails developers.


Can't figure how to make a many-to-many self-join relation

  • Please log in to reply
No replies to this topic

#1 mpepito13



  • Members
  • 1 posts

Posted 05 August 2014 - 09:32 AM

Hi there !


This is my first post here and as a newbie I hope my question won't be to dumb.

I like rails because of its clean syntax (thank you ruby!) but it is so big that I can't figure how doing my stuff done…


I have the following table technics :

create_table :technics do |t|
  t.belongs_to :master, index: true
  t.belongs_to :body_part, index: true
  t.string :name
  t.text :explanation

I would like to be able to create and display technic's variant.


A technic has many variants but it is also the variation of many others so I thought using a has_and_belongs_to_many :variants association.


I could also identify the variants as they have the same master_id & body_part_id so I tried to use a scope.


I also tried to follow that : http://stackoverflow...-model-in-rails


In the end I'm always finding myself stuck…

The only thing nice I like so far is my resource routes as this is exactly what I need :

resources :technics
  resources :variants, shallow: true

But I think I would need a variants table to make it works ?


As you see I am really lost so if anyone could help me it would be really cool !!



Best regards, 


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users