[英]How to display data from multiple tables in one line in rails
I have two tables - Reports and Subjects.我有两个表 - 报告和主题。 How can query and display data from two tables in one line in Rails?
Rails 如何在一行中查询和显示两个表中的数据?
Here are the relationships:以下是关系:
class Subject < ActiveRecord::Base
has_many :term_reports
end
class TermReport < ActiveRecord::Base
belongs_to :subject
end
Here are the schemas:以下是架构:
create_table "subjects", force: true do |t|
t.string "name"
t.integer "user_id"
t.date "createdate"
end
create_table "term_reports", force: true do |t|
t.string "student_studentid"
t.integer "subject_id"
t.integer "score"
t.integer "position"
t.integer "term"
t.integer "year"
t.integer "user_id"
t.date "ceatedate"
t.string "remark"
t.string "form_class"
end
Now I want the data is this format:现在我希望数据是这种格式:
Sujectname score position etc..
Note that the subject name is stored in Sujects table.请注意,主题名称存储在 Sujects 表中。 The bolow code is producing data from one table only:
bolow 代码仅从一张表中生成数据:
@student_subject = TermReport.includes(:subject).all
@student_subject = TermReport.joins(:subject).all
However, this sql is working very well in Workbench but displaying data from one table if I use find_by_sql.但是,此 sql 在 Workbench 中运行良好,但如果我使用 find_by_sql,则会显示一张表中的数据。 Help
帮助
select * from term_reports t, subjects s
where t.subject_id = s.id
This should work just fine you can try it on your console:这应该可以正常工作,您可以在控制台上尝试:
TermReport.includes(:subject).all.each do |term|
puts "#{term.subject.name} #{term.score} #{term.position}"
end
And TermReport.all is enough I don't think you need includes(:subject). TermReport.all 就足够了,我认为您不需要包含(:主题)。
Thanks Smek.谢谢斯梅克。 Here is my controller action:
这是我的控制器操作:
def search_student_results
@student_subject = TermReport.where("student_studentid =? and year =? and term =? and form_class =?",
params[:studentid], params[:year], params[:term], params[:formclass])
respond_to do |format|
format.js {render json: @student_subject}
end
end
And here is my ajax call:这是我的 ajax 调用:
$(function() {
$('#subject_search_btn').click(function() {
var student_id = $('#search').val();
var year = $('#year').val();
var term = $('#term').val();
var classs = $('#formclass').val();
$.ajax({
"url": "/search_student_results",
"data": {"studentid":student_id, "year":year, "term":term, "formclass":classs},
"type": "get",
"dataType": "json",
"success": function(data) {
var trHTML = '';
$.each(data, function (i, item) {
trHTML += '<tr>' +
'<td>' + item.subject_id + '</td>' +
'<td>' + item.score + '</td>' +
'<td>' + item.position + '</td>' +
'<td>' + item.remark + '</td>' +
'</tr>';
});
$('#subject_tbody').append(trHTML);
},
"error": function() {
alert('Student not found');
}
});
});
});
How can I replace item.subject_id with item.subject.name?如何用 item.subject.name 替换 item.subject_id? Thanks
谢谢
So to answer your last question.所以回答你的最后一个问题。
You can change:你可以改变:
format.js {render json: @student_subject}
With:和:
format.json do
render json: @student_subject.as_json(include: { subject: { only: :name } })
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.