可以对“belong_to"建模另外两个模型并具有嵌套关系? [英] can model "belong_to" two other models and have a nested relationship?

查看:43
本文介绍了可以对“belong_to"建模另外两个模型并具有嵌套关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个模型可以属于,两个模型并且有嵌套关系吗?

Is it possible for a model to belong_to, two models and have a nested relationship?

即我想要的

class trainer
has_many :appointments
end

class appointment
belong_to :trainer, :customer
end

class customer
has_many :appointments
end

目前我只有嵌套的客户和约会模型,例如我所拥有的:

at the moment i have only the customer and appointment models which are nested e.g of what i have:

create 方法如下所示:

create method looks like this:

  def create
    @appointment = @customer.appointments.build(params[:appointment])

    respond_to do |format|
      if @appointment.save
        format.html { redirect_to([@customer, @appointment], :notice => 'Appointment was successfully created.') }
        format.xml  { render :xml => @appointment, :status => :created, :location => @appointment }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @appointment.errors, :status => :unprocessable_entity }
      end
    end
  end

在我有的路线中:

  map.resources :patients, :has_many => [ :appointments, :visits ]

1 个模型可以有 2 个嵌套关系吗?如果预约也属于培训师和客户,我需要将我的创建方法更改为什么?

is it possible to have 2 nested relationships for 1 model? what would i have to change my create method to, if appointment also belonged to trainer as well as customer?

谢谢

推荐答案

假设您正在使用 ActiveRecord:让一个模型属于多个其他模型当然是可能的(但是您需要为每个模型指定一个belongs_to 语句关系).

Assuming that you're using ActiveRecord: Having a model belong to more than one other model is possible of course (however you need to specify one belongs_to statement for each relation).

class Appointment < ActiveRecord::Base
  belongs_to :trainer
  belongs_to :customer
end

belongs_to 关系并不一定意味着该记录实际上与其他记录相关;它也可以为零.因此,您可以有属于培训师但没有客户的约会,反之亦然.

A belongs_to relation does not necessarily mean that the record actually has that other record related; it can also be nil. So you can have appointments that belong to a trainer but no customer and vice versa.

实际上,您甚至可以既没有培训师也没有客户,也可以同时拥有培训师和客户——如果这违反了您的业务逻辑,您可能需要添加验证来防止这种情况发生.

Actually you can even have neither a trainer nor a customer or both a trainer and a customer as well this way - if this violates your business logic, you might want to add a validation to prevent this.

您现有的控制器创建方法应该继续工作,您只需要添加训练器记录的处理.您甚至可以通过抽象培训师和客户来使用相同的控制器来处理培训师和客户的任命,例如变成这样的人:

Your existing controller create method should continue to work like it is, you just need to add the handling of trainer records. You can even use the same controller for handling appointment of trainers and customers by abstracting trainers and customers, e.g. into a person like this:

class AppointmentsController < ApplicationController

  def create
    @appointment = person.appointments.build(params[:appointment])
    # ...
  end

protected

  def person
    @person ||=
      if params[:trainer_id]
        Trainer.find(params[:trainer_id])
      elsif params[:customer_id]
        Customer.find(params[:customer_id])
      end
  end
end

这样,你可以为两条路由使用相同的 AppointmentsController

This way, you can use the same AppointmentsController for both routes

# Use AppointmentsController for /trainers/123/appointments
# as well as for /customers/123/appointments
map.resources :trainers, :has_many => :appointments
map.resources :customers, :has_many => :appointments

当然,这只有在培训师预约和客户预约背后的逻辑和观点几乎相同时才有意义.如果没有,您也可以使用不同的控制器

Of course, this only makes sense if the logic and views behind trainer appointments and customer appointments are almost the same. If not, you can also use different controllers

# Use TrainerAppointmentsController for /trainers/123/appointments and
# CustomerAppointmentsController for /customers/123/appointments
map.resources :trainers do |trainer|
  trainer.resources :appointments, :controller => 'trainer_appointments'
end
map.resources :customers do |customer|
  customer.resources :appointments, :controller => 'customer_appointments'
end

这篇关于可以对“belong_to"建模另外两个模型并具有嵌套关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆