Search code examples
ruby-on-railsviewrelationships

Rails - data view from has_many and Has_many_and_belongs_to_many


I'm trying to implement this project:

http://img7.imagebanana.com/img/cnb46ti2/relationships.png

  • I want to let view the skills of an employee on the employee's show page
  • An employee has a position, and every position has skills which an employee of this position needs to know
  • so if I understand right, positions and skills have an n:m relationship, and they need a join table for a has_many_and_belongs_to_many relationship. Because a position includes many skills and every skill belongs to many positions.

now my questions

  1. the position_skill-table -> is it better to use a has_and_belongs_to_many relationship, so this table has no own id or is it better to use a has_many :through relationship? I guess it's better do use a has_and_belongs_to_many relationship, because this relationship table will not have any further information inside than just the two keys. Am I right?
  2. if I take a has_and_belongs_to_many - relationship, is that the only thing I need to write into the models?

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?

  • if that's right, how can I let the skills view on employee's show page? An employee has 1 position, and this position has several skills... What for code do I need to write into the show.html.erb of employee? Something like <%= 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.


Solution

    1. 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.

    2. 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 } }