Ruby on rails can model";属于;其他两个模型和是否有嵌套关系?
一个模型是否可能属于两个模型并具有嵌套关系 i、 我想要什么Ruby on rails can model";属于;其他两个模型和是否有嵌套关系?,ruby-on-rails,Ruby On Rails,一个模型是否可能属于两个模型并具有嵌套关系 i、 我想要什么 class trainer has_many :appointments end class appointment belong_to :trainer, :customer end class customer has_many :appointments end 目前,我只有嵌套的客户和约会模型,例如我拥有的: create方法如下所示: def create @appointment = @customer.
class trainer
has_many :appointments
end
class appointment
belong_to :trainer, :customer
end
class customer
has_many :appointments
end
目前,我只有嵌套的客户和约会模型,例如我拥有的:
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
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
在路线上,我有:
map.resources :patients, :has_many => [ :appointments, :visits ]
一个模型可以有两个嵌套关系吗?如果预约同时属于培训师和客户,我必须将我的创建方法更改为什么
感谢您假设您使用的是ActiveRecord:当然,让一个模型属于多个其他模型是可能的(但是您需要为每个关系指定一个“属于”语句) 当然,只有当培训师预约和客户预约背后的逻辑和观点几乎相同时,这才有意义。如果没有,也可以使用不同的控制器
# 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
+一个精彩的回答!非常感谢。只有一个问题,如果我从客户页面/视图创建约会。如何将培训师ID添加到约会中?我是否可以在视图中加载所有培训师,并允许用户从下拉菜单中选择一名培训师,然后将培训师ID分配给预约中存档的培训师ID?您的问题是关于培训师ID参数还是关于记录属性?trainer_id参数由嵌套的资源路由设置
/trainers/123/约会
将参数[:trainer\u id]
设置为123,/customers/123/约会
将参数[:customer\u id]
设置为123。约会记录上的trainer\u id属性由has\u many关联设置@appointment=person.appointment.build
将自动将@appointment.trainer\u id
或@appointment.customer\u id
设置为person.id
,具体取决于person是什么类别的人(要更改默认值,有一个:foreign\u key选项用于所属的人)。假设我转到一个客户档案,并从他的档案中创建约会(它将从客户的id参数中获取id),但我如何为该约会指派一名负责该约会的培训师。如果我有10名培训师,如何将预约设置为其中一名?我想允许用户从下拉菜单中选择一名培训师,然后将该培训师的ID设置为“培训师ID”字段?最简单的方法是,让预约表单提交参数[:预约][:培训师ID]的值以及其他字段。更新操作将设置trainer_id属性,并保存关联。有一些表单帮助程序可以从集合中创建下拉选择框,因此用户可以获得一个不错的培训师名称选择collection\u select
可能是您要在视图中使用的帮助器:这已经很老了,但仍然出现了“two-belish-to-relationships”-值得注意的是,由于Rails 5,保存子实例时现在需要belish\u-to
模型;如果没有关联的模型,保存将失败。可通过归属\u to:trainer可选:true忽略-
# 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
# 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