I'm trying to implement this project:
http://img7.imagebanana.com/img/cnb46ti2/relationships.png
now my questions
a) class Position < ActiveRecord :: Base (...) has_and_belongs_to_many :skills (...)
b) class Skill < ActiveRecord :: Base (...) has_and_belongs_to_many :positions (...)
c) into db\migrate def self.up create_table :positon_skill, :id => false do |t| (...)
and after that, the positions and skills are connected with each other? Is that right? Did I forget something?
<%= employee.position.skill %>
? Do I also need to render something? Sorry, I'm very confused and I think I read too much information in web... Or is there any description in web which exactly describes what I need for?thanks alot in advance and sorry for that redundant question.
If you're sure you aren't going to want to add any information later to the position_skills
table, has_and_belongs_to_many
will work fine. However, has_many :through
is far more flexible if you change your mind later and isn't much harder to set up.
If you use has_and_belongs_to_many
, you only need association declarations in the models and the database table with position_id:integer
and skill_id:integer
fields. Seems like you've got that already.
To be able to access employee.position.skills
in your view, you need to eagerly load the employee's associations. Try something like the following:
class EmployeesController < ApplicationController
...
def show
@employee = Employee.find(params[:id], :include => { :position => :skills })
end
...
end
I think that should work if you stick with has_and_belongs_to_many
, but if you go for has_many :through
(which I recommend), you'll need to use :include => { :position => { :position_skills => :skills } }