Many to Many rails form

0

I would like to know how to do a form many to many in rails.

Here are my models:

class Course < ActiveRecord::Base
  has_and_belongs_to_many :notices, dependent: :destroy
  has_and_belongs_to_many :internships, dependent: :destroy
end

class Internship < ActiveRecord::Base
  belongs_to :enterprise
  has_and_belongs_to_many :courses
end

I also have another question about the same subject. Assuming I have 3 internship-related courses.

When I use action update and update related courses to 2, will rails automatically delete that relationship that is no longer present in the update?

    
asked by anonymous 30.08.2015 / 04:58

1 answer

0

For Many To Many in your case it could be:

  

course

     

has_and_belongs_to_many: internships,: join_table = > : course_internships

     

internship

     

has_many: course

     

course_internship

     

belongs_to: courses

     

belongs_to: internships

In the course_internships table there would be 3 fields, which are:

  

course_internships

     

id

     

courses_id

     

internships_id

Maybe this will help you in many to many!

    
17.12.2015 / 17:47