ruby-on-rails – 输出深层嵌套的json关联
发布时间:2020-12-17 02:28:32 所属栏目:百科 来源:网络整理
导读:我有一个具有以下结构的rails3应用程序 Users has_many :tasks has_many :staff has_many :managersTasks has_one :locationLocations has_many :staff has_one :managerStaff belongs_to :users belongs_to :locationManagers belongs_to :users belongs_to
我有一个具有以下结构的rails3应用程序
Users has_many :tasks has_many :staff has_many :managers Tasks has_one :location Locations has_many :staff has_one :manager Staff belongs_to :users belongs_to :location Managers belongs_to :users belongs_to :location 现在在我的json输出中,我正在尝试获取用户,他们的任务,每个任务位置以及位置员工和经理. 我有 @User = User.find(params[:id],:include{:tasks=>:location}) 这将输出用户和任务的位置,但不输出任务本身. 还有任何进一步包括我尝试添加,导致例如错误 @User = User.find(params[:id],:include{:tasks=>:location=>:staff}) 我收到“意外的tASSOC,期待}的错误. 写这个的正确方法是什么,所以我得到了这个用户的所有相关数据? 解决方法
您应该能够使用此命令获取用户和关联的对象:
@user = User.includes(:tasks => {:location => :staff}).find(params[:id]) 你会看到一些查询执行.第一个将检索用户,第二个将检索相关任务,第三个将检索相关位置,第四个将检索相关人员.您会注意到在键入以下任何内容时不会执行任何查询: @user.tasks.first.location.staff @user.tasks.first.location @user.tasks.first (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- c# – 使用CompileAssemblyFromSource加载自定义
- ruby-on-rails – 如何在Factory Girl Rails中使
- 使用mvn 下载 依赖包的源代码
- oracle sql rollback savepoint
- ruby-on-rails – respond_to do | format | for
- 如果使用标准POSIX正则表达式前面有另一个字符串
- 基于xml的springMVC请求与数据返回
- Oracle In和exists not in和not exists的比较分析
- JSONObject与JSONArray的使用
- c – 在编译的ROOT(CERN)应用程序中将TCanvas绘制
热点阅读