Rails 5 - n + 1查询问题在simple_form gem中
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Rails 5 - n + 1查询问题在simple_form gem中相关的知识,希望对你有一定的参考价值。
我正在尝试用我的Rails中的Bullet gem解决每个n + 1问题,但我无法弄清楚这个特殊问题:
_form.html.erb
<%= simple_form_for([:admin, @course, @lesson]) do |f| %>
<div class="form-inputs">
<div class="col-md-6">
<%= f.input :start_at, minute_step: 5, start_year: Date.today.year %>
</div>
<div class="col-md-6">
<%= f.input :end_at, minute_step: 5, start_year: Date.today.year %>
</div>
<div class="col-md-6">
<!--Here is the problem-->
<%= f.association :room, :label_method => lambda { |room| "#{room.title} (#{room.building.title})"}%>
<!--Here is the problem-->
</div>
<div class="col-md-6">
<%= f.association :teacher, :label_method => lambda { |teacher| "#{teacher.first_name} #{teacher.last_name}"},
collection: @course.teachers%>
</div>
</div>
<div class="col-md-12">
<div class="form-actions text-center">
<%= f.submit class: "btn btn-primary btn-lg" %>
</div>
</div>
<% end %>
表单呈现给new.html.erb和edit.html.erb。问题发生在我想要获得room.building.title的房间协会。
这是截图:qazxsw poi
如您所见,我只想显示具有中心名称的房间名称,但Bullet会抛出错误。问题是我正在使用lambda函数,因此在控制器中创建变量没有帮助(或者至少我不知道如何正确创建它),因为问题发生在选择框内。我在这里包括我的控制器:
lessons_controller.rb
(在这种情况下与我们相关的行动是新的和编辑)
以下是相关型号:
room.rb
module Admin
class LessonsController < Admin::AdminController
helper_method :convert_time, :convert_day
before_action :set_course
before_action :set_lesson, only: [:show, :edit, :update, :destroy]
def index
@lessons = Lesson.includes(:teacher,:course, :room => :building).where(course_id: @course).paginate(page: params[:page], per_page: 10)
# @course_lessons = @course.lessons.paginate(page: params[:page], per_page: 10)
end
def new
#@room = Room.all.includes(:building)
@lesson = Lesson.new
end
def create
@lesson = Lesson.new(lesson_params)
@lesson.course = @course
if @lesson.save
flash[:success] = "Lesson was created"
redirect_to admin_course_lessons_path(@course)
else
render 'new'
end
end
def edit
end
def update
if @lesson.update(lesson_params)
flash[:success] = "Lesson was updated"
redirect_to admin_course_lessons_path(@course)
else
render 'edit'
end
end
def show
end
def destroy
@lesson.destroy
flash[:danger] = "Lesson was deleted"
redirect_to admin_course_lessons_path(@course)
end
private
def set_course
@course = Course.find(params[:course_id])
end
def set_lesson
@lesson = Lesson.find(params[:id])
end
def lesson_params
params.require(:lesson).permit(:start_at,:end_at, :room_id, :teacher_id)
end
end
end
building.rb
class Room < ApplicationRecord
belongs_to :building
has_many :lessons, dependent: :destroy
before_save {self.title = title.upcase_first}
before_save {self.code = code.upcase}
validates :title, presence: true,
length: { minimum: 3, maximum: 50},
uniqueness: {case_sensitive: false}
validates :code, presence: true,
length: {minimum: 2},
uniqueness: {case_sensitive: false}
validates :building_id, presence: true
end
抱歉这篇长篇文章和我缺乏知识。我将不胜感激任何反馈。
您可以在class Building < ApplicationRecord
has_many :rooms, dependent: :destroy
before_save {self.title = title.upcase_first}
before_save {self.code = code.upcase}
validates :title, presence: true,
length: { minimum: 3, maximum: 50},
uniqueness: true
validates :code, presence: true,
length: {minimum: 3},
uniqueness: {case_sensitive: false}
end
中使用collection
指定includes
选项。像这样:
f.association
以上是关于Rails 5 - n + 1查询问题在simple_form gem中的主要内容,如果未能解决你的问题,请参考以下文章
Rails 5 simple_form 将单选按钮标记为必需,如果未填写则阻止提交表单
提交按钮根本不起作用 Simple_Forms Rails 4
MongoMapper 避免在 Ruby on Rails 中导致 N+1 查询