Rails - data view from has_many and Has_many_and_belongs_to_many

旧时模样 提交于 2019-12-12 04:32:17

问题


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.


回答1:


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




回答2:


This is what it looks like in your diagram. Consider the following:

class Employee < ActiveRecord :: Base
  belongs_to :position
  ...
end

class Position < ActiveRecord :: Base
  has_many :employees
  has_many :position_skills
  has_many :skills, :through => :position_skills
  ...
end    

class PositionSkill < ActiveRecord :: Base
  belongs_to :position
  belongs_to :skill
  ...
end

class Skill < ActiveRecord :: Base
  has_many :position_skills
  has_many :positions, :through => :position_skills
  ...
end

The only problem with this is that an employee is tied to a single position. While this position has many skills through positions. I would change it to position belongs_to employee and employee has_many positions. This leaves it open to track employees that move from one position to the next. Let me know if you need further info on that.



来源:https://stackoverflow.com/questions/6390262/rails-data-view-from-has-many-and-has-many-and-belongs-to-many

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!