如何从关联中的链接访问数据? [英] How do I access the data from a link in an association?

查看:125
本文介绍了如何从关联中的链接访问数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有:

Aulas
has_many :grades
has_many :students, :through => :grades

Students
has_many :grades
has_many :aulas, :through => :grades

Grades
belongs_to :aula
belongs_to :student

我想从该光环和该光环内特定学生之间的连接处显示grades.name.这行不通,但是您会理解我想要的:

I want to show grades.name from the conection between that Aula and a specific Student inside the Aula. This doesn't work, but you will understand what I want:

<% aula.students.each do |student| %>
  <%= link_to student.name, student %>-<%= student.grade.name %>
<% end %>

好吧,如果我做students.grades,我就在Aula(id:68)里面,我从所有Aulas那里获得成绩

Ok so, i'm inside Aula (id:68) if I do students.grades, I get the grades from all Aulas

[#<Grade id: 51, name: AA1, student_id: 22, aula_id: 68 >,#<Grade id: 52, name: AA2, student_id: 22, aula_id: 69 >,#<Grade id: 53, name: AA3, student_id: 22, aula_id: 70 >]

我如何仅获得与此Aula相关的年级名称(aula_id:68)?

How do i get only the name of the grade related to this Aula (aula_id:68)?

推荐答案

好.我想,我可以通过年级来获得学生的名字,而不是通过学生来获得年级的名字,所以我做到了

Ok. I figured that instead of getting the name of the Grade throught Students, i can get the student through grades, so I did

<% aula.grades.each do |grade| %>
  <%= link_to grade.student.name, student %>-<%= grade.name %>
<% end %>

现在它好起来了.

这篇关于如何从关联中的链接访问数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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