Rails - has_many 和 Has_many_and_belongs_to_many 的数据视图

发布于 2024-11-16 03:19:57 字数 1243 浏览 1 评论 0原文

我正在尝试实现这个项目:

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

  • 我想让员工在员工展示页查看员工的技能
  • 员工有一个职位,每个职位都有该职位员工需要具备的技能 如果我理解正确的话
  • ,职位和技能具有 n:m 关系,并且它们需要一个连接表来实现 has_many_and_belongs_to_many 关系。因为一个职位包含了很多技能,而每一个技能又属于很多职位。

现在我的问题是

  1. position_skill-table ->是使用 has_and_belongs_to_many 关系更好,因此该表没有自己的 id,还是使用 has_many :through 关系更好?我想最好使用 has_and_belongs_to_many 关系,因为这个关系表除了两个键之外不会有任何进一步的信息。我说得对吗?
  2. 如果我采用 has_and_belongs_to_many 关系,这是我需要写入模型的唯一内容吗?

a) 类位置 < ActiveRecord :: Base (...) has_and_belongs_to_many :skills (...)

b) class Skill class Skill ActiveRecord :: Base (...) has_and_belongs_to_many :positions (...)

c) 进入 db\migrate def self.up create_table :positon_skill, :id =>假|t| (...) 然后职位和技能是有联系的吗?是这样吗?我是不是忘记了什么?

  • 如果是的话,我怎样才能让技能在员工的展示页面上查看?一个员工有1个职位,这个职位有几个技能...我需要在员工的show.html.erb中写入什么代码?像<%=employee.position.skill %>之类的东西?我还需要渲染一些东西吗?抱歉,我很困惑,我想我在网络上阅读了太多信息...或者网络上是否有任何描述准确描述了我需要的内容?

提前非常感谢,并对这个多余的问题表示歉意。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ゝ杯具 2024-11-23 03:19:57
  1. 如果您确定以后不想向 position_skills 表中添加任何信息,has_and_belongs_to_many 就可以正常工作。但是,如果您稍后改变主意,has_many :through 会更加灵活,并且设置起来也不会困难。

  2. 如果您使用has_and_belongs_to_many,则只需要在模型和数据库表中使用position_id:integerskill_id:integer字段进行关联声明。看起来你已经明白了。

为了能够在您的视图中访问 employee.position.skills,您需要立即加载员工的关联。尝试如下操作:

class EmployeesController < ApplicationController
  ...
  def show
    @employee = Employee.find(params[:id], :include => { :position => :skills })
  end
  ...
end

我认为如果您坚持使用 has_and_belongs_to_many ,那么应该可行,但如果您选择 has_many :through (我推荐),则需要使用 :include =>; {:位置=> { :position_skills =>; :技能 } }

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

染墨丶若流云 2024-11-23 03:19:57

这就是你的图表中的样子。考虑以下因素:

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

唯一的问题是员工被束缚在一个职位上。虽然这个职位通过职位有很多技能。我会将其更改为职位 belongs_to 员工和员工 has_many 职位。这样就可以跟踪从一个职位跳槽到下一个职位的员工。如果您需要更多信息,请告诉我。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文