Rails - 来自 has_many 和 Has_many_and_belongs_to_many 的数据视图 [英] Rails - data view from has_many and Has_many_and_belongs_to_many

查看:68
本文介绍了Rails - 来自 has_many 和 Has_many_and_belongs_to_many 的数据视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to implement this project:

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) class 位置

b) 职业技能

c) 进入 db\migrate def self.up create_table :positon_skill, :id =>假做 |t|(...)之后,职位和技能是相互关联的?是对的吗?我是不是忘记了什么?

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?

  • 如果是这样,我如何让技能在员工的显示页面上查看?一个员工有1个职位,这个职位有几个技能... 员工的show.html.erb里需要写什么代码?类似<%=employee.position.skill %>?我还需要渲染一些东西吗?抱歉,我很困惑,我认为我在网络上阅读了太多信息......或者网络中是否有任何描述准确描述了我需要什么?
  • 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. 如果您确定以后不想将任何信息添加到 position_skills 表中,has_and_belongs_to_many 将正常工作.但是,如果您以后改变主意,has_many :through 会灵活得多,而且设置起来也不难.

  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.

如果你使用has_and_belongs_to_many,你只需要在模型和数据库表中用position_id:integerskill_id:integer进行关联声明代码> 字段.好像你已经知道了.

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.

为了能够在您的视图中访问 employee.position.skills,您需要急切地加载员工的关联.请尝试以下操作:

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

我认为如果你坚持使用 has_and_belongs_to_many,那应该可以,但如果你选择 has_many :through(我推荐),你需要使用 :include =>{:位置=>{ :position_skills =>:skills } }

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

这篇关于Rails - 来自 has_many 和 Has_many_and_belongs_to_many 的数据视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆