I made a relationship with the three models using has_many :through:
class Curriculum class < ActiveRecord::Base
has_many :interests
has_many :vacancies,: through => :interests
end
class Vacancy class < ActiveRecord::Base
has_many :interests
has_many :resumes,: through => :interests
end
class Interest < ActiveRecord:: Base
belongs_to :vacancy
belongs_to :curriculum
end
And to create curriculum and vacancy, I create them by administrative, i need to know how can i create the interest to the id of the vacancy, and how it will be logged on the system I have to get the id of it and make the relationship in creating a new bank interest. I wonder how I can program it to do so, and I wonder how the controller will get the create action, and what better way to do this.
First, try to read the whole “Guide to Rails on Associations”, especially the part about
has_many :through. Then check your schema if your db is migrated and contains for the tableintereststhe necessary foreign keys tocurriculumsandvacanciescalledcurriculum_idandvacancy_id.If that is all in place, the following code will create the relationship between two objects:
The last two lines creates an interest between
@currand@vacand store that on the database. So you should not use IDs and handle them directly, but work with objects instead.The second part now is to provide a UI to allow the definition (and removal) of interests between curricula and vacancies. The base flow here is:
See the (older) podcast Railscast #52 how to do that in a similar context. Or see the example for has_many :through with checkboxes.
An alternative way would be to use JQuery autocomplete, and add so interests one-by-one. See the nice podcast Railscast #258 which uses JQuery Tokeninput for that.