Ruby on rails 二者通过同一模型有许多关系
我面临一个问题,我不知道如何解决它 我有三种型号Ruby on rails 二者通过同一模型有许多关系,ruby-on-rails,ruby,ruby-on-rails-3,Ruby On Rails,Ruby,Ruby On Rails 3,我面临一个问题,我不知道如何解决它 我有三种型号 老师 房间 小时数 我需要通过同一个模型将它们关联起来。到现在为止,我有一个第四个模型叫做schedules,我有两个外键。教师id和小时id,这是完美的工作,但我还需要通过这个模型关联房间 这是我的密码: teacher.rb class Teacher < ApplicationRecord scope :active, -> {where(deleted_at: nil)} has_many :schedules,
- 老师
- 房间
- 小时数
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
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
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
class Schedule < ApplicationRecord
belongs_to :teacher
belongs_to :lesson_hour
end
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
将课时与教师链接的表格如下:
<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>
有争议的
所以,我的问题是,如何在日程模型中包含房间关联
提前感谢当您需要添加两个has\u many时,您需要指定外键和类名,以便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
class two有许多
当需要添加两个has\u many时,需要指定外键和类名,以便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
class two有许多
很好,谢谢Andre。但在我看来,我该如何管理它呢。我的观点是一个新老师的表单,我需要在同一表单中添加教室和课时。到现在为止,我只有上课时了。很好,谢谢你,安德烈。但在我看来,我该如何管理它呢。我的观点是一个新老师的表单,我需要在同一表单中添加教室和课时。到现在为止,我只有上课时了。