简体   繁体   English

如何在rails中的一行中显示来自多个表的数据

[英]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.

相关问题 如何将数据从多个表显示到dataGridView - how to display data from multiple tables to dataGridView 如何从多个表中查询数据并显示相对于其上次工作日期的每个设备专用的一条记录 - how to query data from multiple tables and display one record specific to each device relative to their last working date 如何将数据从一个mySQL表显示到php中的多个表中? - How do I display data from one mySQL table into multiple tables in php? 如何比较两个表中的数据并显示一个表中的数据 - How to compare data in two tables and display data from one table 如何显示来自不同数据表的数据? 一对多关系 - How to display data from different data tables? One to many reationship 以表格形式显示来自 Rails 3 上 Ruby 中 2 个表的数据 - Display data in form from 2 tables in Ruby on Rails 3 如何将数据从不同的表列显示到一个表列 - how to display data from different tables column into one table column 如何从Ruby on Rails的多个表中获取数据? - How to get data from multiple tables in Ruby on Rails? MySQL如何只显示一次来自多个表的数据? - MySql how to display data from multiple tables only once? 如何显示来自多个表的有意义的数据(mySQL) - How to display meaningful data from multiple tables (mySQL)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM