Form Many to Many Rails

Asked

Viewed 48 times

0

I’d like to know how to make one 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 regarding the same subject. Assuming I have 3 courses related to the internship.

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

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_internship

belongs_to :

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!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.