具有多个连接的活动记录中的自定义查询

时间:2017-10-20 02:50:19

标签: ruby-on-rails activerecord

我对rails和活动记录都很陌生,所以我可能做错了,但是...使用下面的模式,我想在外部do循环中创建一个课程列表,然后列出使用内部do循环注册课程的每个学生。我不确定这种方法是否有效,但是这个错误不断出现:

  

ActionView :: Template :: Error(未定义的方法`注册' for#):

似乎是关联。我做错了什么?

教授节目页面:

<div class="col-md-8">
  <h2 class="card-title"><%= @professor.name %></h2>

    <% @courses_taught.each do |course| %>
        <div class="card mb-4 card-header">
          <img class="card-img-top" src="http://placehold.it/750x300" alt="Card image cap">
          <h3 class="card-text"><%= course.title %></h3>
        </div>
        <div class="card-body">
          <% course.sections.enrollments.students.each do |student| %>
              <p><% student.name %></p>
          <% end %>
        </div>
    <% end %>

</div>

模型:

注册

class Enrollment < ApplicationRecord
  belongs_to :section
  belongs_to :student
end

学生:

class Student < ApplicationRecord
  has_many :enrollments
end

教授:

class Section < ApplicationRecord
  has_many :enrollments
  belongs_to :professor
  belongs_to :course

  validates_uniqueness_of :professor_id, scope: :course_id

  scope :by_professor_id, ->(prof_id) { where('professor_id = ?', prof_id) }
end

课程:

class Course < ApplicationRecord
  enum status: { planning: 0, offered: 1 }

  scope :offered, -> { where(status: 1) }
  scope :planning, -> { where(status: 0) }

  belongs_to :department
  has_many :sections
  has_many :professors, through: :sections

  validates :title, :number, :status, :description, presence: true
  validates :description, length: { in: 10..500 }
  validates :title, :number, uniqueness: { case_sensitive: false }

  def self.search(term)
    if term
      where('title LIKE ?', "%#{term}%").order('title DESC')
    else
      order('title ASC')
    end
  end

  def self.taught_by(professor_id)
    Course
        .joins(:sections)
        .joins(:professors)
        .where(sections: { professor_id: professor_id })
        .select('distinct courses.*')
  end

end

架构:

ActiveRecord::Schema.define(version: 20171013201907) do

  create_table "courses", force: :cascade do |t|
    t.string "title"
    t.text "description"
    t.string "number"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "status", default: 0
    t.integer "department_id"
    t.index ["department_id"], name: "index_courses_on_department_id"
  end

  create_table "departments", force: :cascade do |t|
    t.string "name"
    t.text "description"
    t.text "main_image"
    t.text "thumb_image"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "enrollments", force: :cascade do |t|
    t.integer "section_id"
    t.integer "student_id"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.index ["section_id"], name: "index_enrollments_on_section_id"
    t.index ["student_id"], name: "index_enrollments_on_student_id"
  end

  create_table "professors", force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "status", default: 0
    t.integer "department_id"
    t.text "bio"
    t.index ["department_id"], name: "index_professors_on_department_id"
  end

  create_table "sections", force: :cascade do |t|
    t.integer "number"
    t.integer "max_enrollment"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer "professor_id"
    t.integer "course_id"
    t.string "room"
    t.index ["course_id"], name: "index_sections_on_course_id"
    t.index ["professor_id", "course_id"], name: "index_sections_on_professor_id_and_course_id", unique: true
    t.index ["professor_id"], name: "index_sections_on_professor_id"
  end

  create_table "students", force: :cascade do |t|
    t.string "name"
    t.decimal "gpa"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "users", force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "name"
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer "sign_in_count", default: 0, null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string "current_sign_in_ip"
    t.string "last_sign_in_ip"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "roles"
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

end

3 个答案:

答案 0 :(得分:4)

注册belongs_to用户,没有必要在单个记录上使用每个而不是它会抛出错误。您可以使用以下代码。

@model IEnumerable <ReportObj>
@{
    Layout = "~/Views/Shared/ViewLayout.cshtml";
}

<button type="button" class="ladda-button btn btn-warning" data-style="contract" id="btnSearch">Search</button>&nbsp;&nbsp;
  <div id="datasection">
     <table id="datatable-buttons" class="table table-responsive table-bordered">
        <thead>
           <tr class="titlerow">
            @{int i = 0;
              foreach (var property in Model.GetType().GetGenericArguments()[0].GetProperties())
              {
                 if (i.Equals(1))
                 {
                   <th class="no-sort" style="background-color:   #5fbeaa; color: white;">
                   <input type="checkbox" id="SelCntrl" name="allcb" />Quoteid
                   </th>
                  }
                  else
                  {
                    <th class="no-sort" style="background-color:   #5fbeaa; color: white;">@property.Name</th>    
                  }
                i++;
               }
             }
             </tr>
          </thead>
         <tbody>
         @foreach (var modelItem in Model)
         {
             <tr class="contentrow">
             @{
               int j = 0; string cbname = "cb";
               foreach (var property in modelItem.GetType().GetProperties())
               {
                  if (j.Equals(1))
                  {
                    <td>
                    <input class="checkbox checkbox-inverse"
                           type="checkbox"
                           value="@property.GetValue(modelItem)"
                           id="@String.Concat("cb", @property.GetValue(modelItem))"
                           name="@String.Concat("cb", @property.GetValue(modelItem))"
                           onclick="Chboxclick(this, this.checked, '@String.Concat("cb", @property.GetValue(modelItem))')" />
                    @property.GetValue(modelItem)
                </td>
                }
                else
               {<td>@property.GetValue(modelItem)</td>}
               j++;
         }
       }
     </tr>
    }
    </tbody>
  </table>
 </div>
<script>    
    $("#btnSearch").click(function () {
        SearchBtnFunc();
    });

   function SearchBtnFunc()
    {
      var table = $('#datatable-buttons').DataTable();
        table.destroy();
        $("#datasection").html("LOADING....");
        table.deferRender = true;
        table.serverSide = true;

        $.ajax({
            url: "/mycontroller/Search?Param=" ,
            type: "GET",

            success: function (data) {
                $("#datasection").html(data); 
                TableManageButtonsNoSort.init();
            },
            error: function (data) {
                alert('Data Error : ' + data.responseText);
            }
        });

    }
</script>

答案 1 :(得分:2)

肖恩的回答几乎是正确的但是因为报名属于学生 你会使用单数形式enrollment.student。但更简单的说,你可以从注册区内拨打学生。

<div class="col-md-8">
  <h2 class="card-title"><%= @professor.name %></h2>

    <% @courses_taught.each do |course| %>
        <div class="card mb-4 card-header">
          <img class="card-img-top" src="http://placehold.it/750x300" alt="Card image cap">
          <h3 class="card-text"><%= course.title %></h3>
        </div>
        <div class="card-body">
          <% course.sections.each do |section| %>
            <% section.enrollments.each do |enrollment| %>                  
                <p><% enrollment.student.name %></p>                 
            <% end %>
          <% end %>
        </div>
    <% end %>
</div>

答案 2 :(得分:1)

<div class="col-md-8">
  <h2 class="card-title"><%= @professor.name %></h2>

    <% @courses_taught.each do |course| %>
        <div class="card mb-4 card-header">
          <img class="card-img-top" src="http://placehold.it/750x300" alt="Card image cap">
          <h3 class="card-text"><%= course.title %></h3>
        </div>
        <div class="card-body">
          <% course.sections.each do |section| %>
            <% section.enrollments.each do |enrollment| %>
              <p><% enrollment.student.name %></p>
            <% end %>
          <% end %>
        </div>
    <% end %>
</div>