我正面临一个问题,我不知道如何解决它。
我有三个模特
我需要通过相同的模型关联它们。到目前为止,我有一个名为scheduleles的第四个模型,我有两个外键。 teacher_id和hour_id,工作正常,但我还需要通过这个模型关联房间。
这是我的代码:
teacher.rb
class Teacher < ApplicationRecord
scope :active, -> {where(deleted_at: nil)}
has_many :schedules, dependent: :destroy
has_many :lesson_hours, through: :schedules
has_many :teacher_courses, dependent: :destroy
has_many :courses, through: :teacher_courses
has_many :registrations
def check_associations
errors.add(:base, "Há matrículas cadastradas com esse professor. Por favor ajuste antes de deletar.") if self.registrations.active.count > 0
return errors.blank?
end
def delete!
if check_associations
self.deleted_at = Time.current
save!
return true
else
return false
end
end
end
room.rb
class Room < ApplicationRecord
scope :active, -> {where(deleted_at: nil)}
has_many :room_courses, dependent: :destroy
has_many :courses, through: :room_courses
has_many :registrations
def check_associations
errors.add(:base, "Há matrículas cadastradas com essa sala. Por favor ajuste antes de deletar.") if self.registrations.active.count > 0
return errors.blank?
end
def delete!
if check_associations
self.deleted_at = Time.current
save!
return true
else
return false
end
end
end
lesson_hour.rb
class LessonHour < ApplicationRecord
scope :active, -> {where(deleted_at: nil)}
has_many :schedules, dependent: :destroy
has_many :teachers, through: :schedules
has_many :registrations
def check_associations
errors.add(:base, "Há matrículas cadastradas nesse horário. Por favor ajuste antes de deletar.") if self.registrations.active.count > 0
return errors.blank?
end
def delete!
if check_associations
self.deleted_at = Time.current
save!
return true
else
return false
end
end
end
schedule.rb
class Schedule < ApplicationRecord
belongs_to :teacher
belongs_to :lesson_hour
end
所有这些关联都是通过TeachersController
中的表单构建的teachers_controller.rb
def set_lesson_days_and_hours
@lesson_days = LessonHour.select(:weekday)
.group(:weekday)
.order(:weekday)
.to_a
@lesson_hours = {}
@lesson_days.each do |ld|
@lesson_hours[ld.weekday] = LessonHour.active
.select(:id, :start_time)
.order(:start_time)
.where(weekday: ld.weekday)
end
end
def teacher_params
params
.require(:teacher)
.permit(:name, :address, :address_number, :address_complement,
:address_cep, :address_neighborhood, :city, :state, :birthdate,
:email, :cpf, :rg, :phone, :mobile, :started_date, :bank, :agency,
:account_number, :gender, lesson_hour_ids: [], course_ids: [])
end
将lesson_hour链接到教师的表单如下:
<div class="row teacher-schedule">
<h4>Disponibilidade</h4>
<% @lesson_days.each do |ld| %>
<div class="col-md-2 one-per-line teacher-schedule">
<span><strong><%= t(:"date.day_names")[ld.weekday] %></strong></span>
<%= f.collection_check_boxes(:lesson_hour_ids, @lesson_hours[ld.weekday], :id, :start_time) do |check_box| %>
<%= check_box.label class:"label-checkbox" do %>
<%= check_box.check_box + check_box.text.strftime("%H:%M") %>
<% end %>
<% end %>
</div>
<% end %>
</div>
所以,我的问题是,如何在计划模型中包含房间关联?
提前致谢
答案 0 :(得分:2)
当你需要添加两个has_many时,你需要指定foreign_key和class_name,以便rails可以进行连接。
示例:
class TwoHasMany < ApplicationRecord
has_many :scheduled_teachers, foreign_key: :teacher_id, class_name: 'Schedule'
has_many :teachers, through: :scheduled_teachers
has_many :scheduled_lessons, foreign_key: :lesson_hour_id, class_name: 'Schedule'
has_many :lesson_hours, through: :scheduled_lessons
end