问题描述
如果我做这样的事情:
result = Appointment.find( :all, :include => :staff )
logger.debug { result.inspect }
那么它只能打印出预约数据,并没有相关的工作人员数据。如果我得到[0] .staff.inpsect然后我得到场的工作人员数据。
then it only prints out the Appointment data, and not the associated staff data.If I do result[0].staff.inpsect then I get the staff data of course.
现在的问题是我要回这对AJAX的JSON,包括工作人员行。我如何迫使它包括工作人员的行,或者我通过有环和手动创建的东西?
The problem is I want to return this to AJAX as JSON, including the staff rows. How do I force it to include the staff rows, or do I have to loop through and create something manually?
推荐答案
:包括
是 to_json
参数,没有查找
。你需要做的,您的控制器是什么:
:include
is an argument for to_json
, not find
. What you need to do in your controller is this:
def return_json
@appointment = Appointment.find(:all)
respond_to { |format|
format.json { render :json => @appointment.to_json(:include => :staff) }
}
end
您需要设置工作任用和工作人员之间的关联这一点。
You need to setup an association between Appointment and Staff for this to work.
这篇关于如何转换的记录,包括“包括”协会JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!