Is it possible for a model to belong_to, two models and have a nested relationship?
模型是否可以属于,两个模型并具有嵌套关系?
i.e of what i want
即我想要的
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 method looks like this:
create方法如下所示:
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
in routes i have:
在我有的路线:
map.resources :patients, :has_many => [ :appointments, :visits ]
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?
是否可以为1个模型建立2个嵌套关系?如果约会也属于培训师和客户,我还需要改变我的创建方法?
thanks
谢谢
1 个解决方案
#1
21
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).
假设您正在使用ActiveRecord:当然可以使模型属于多个其他模型(但是您需要为每个关系指定一个belongs_to语句)。
class Appointment < ActiveRecord::Base
belongs_to :trainer
belongs_to :customer
end
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.
belongs_to关系并不一定意味着记录实际上与其他记录有关;它也可以是零。因此,您可以预约属于培训师而不是客户,反之亦然。
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
This way, you can use the same AppointmentsController for both routes
这样,您可以对两个路由使用相同的AppointmentsController
# 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
#1
21
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).
假设您正在使用ActiveRecord:当然可以使模型属于多个其他模型(但是您需要为每个关系指定一个belongs_to语句)。
class Appointment < ActiveRecord::Base
belongs_to :trainer
belongs_to :customer
end
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.
belongs_to关系并不一定意味着记录实际上与其他记录有关;它也可以是零。因此,您可以预约属于培训师而不是客户,反之亦然。
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
This way, you can use the same AppointmentsController for both routes
这样,您可以对两个路由使用相同的AppointmentsController
# 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