Ruby on Rails中具有嵌套关系的查询 [英] Ruby on Rails where query with nesting relations

查看:145
本文介绍了Ruby on Rails中具有嵌套关系的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从这个问题中学到了如何在关系中使用where查询.

I learned how to use where query with relations from this question.

Ruby on Rails进行关系查询

但是,我仍然无法使用此套料盒. 如何使Summaries控制器索引起作用?

However, I still can't make it right with this nesting case. How can I make Summaries controllers index work?

型号

User
  has_many :projects, dependent: :destroy
  has_many :reasons, through: :projects
  has_many :summaries, through: :projects, source: :reasons
  has_many :entries, through: :projects

Project
  belongs_to :user
  has_many :reasons
  has_many :entries, through: :reasons

Reasons
  belongs_to :project
  has_many :entries, dependent: :destroy 
  has_many :summaries, dependent: :destroy 

Summary
  belongs_to :reason

Entry
  belongs_to :reason

EntriesController

EntriesController

  # GET /entries
  def index
    entries     = current_user.entries
    updated_at  = params[:updated_at]

    # Filter with updated_at for reloading from mobile app
    if updated_at.present?

      # THIS WORKS!!!!!!!!!!!!!
      entries = entries.joins(:reason).where("reasons.updated_at > ?", Time.at(updated_at.to_i))

    # Get all non deleted objects when logging in from mobile app
    else
      entries = entries.where(deleted: false)
    end

    render json: entries
  end 

SummariesController

SummariesController

  # GET /summaries
  def index
    summaries   = current_user.summaries
    updated_at  = params[:updated_at]

    # Filter with updated_at for reloading from mobile app
    if updated_at.present?



      #THIS DOES NOT WORK, what can I do?????
      summaries = summaries.joins(:reason).where("reasons.updated_at > ?", Time.at(updated_at.to_i))



    # Get all non deleted objects when logging in from mobile app
    else
      summaries = summaries.where(deleted: false)
    end

    render json: summaries
  end  

@iOS上的错误日志

@Error log on iOS

错误:错误Domain = com.alamofire.error.serialization.response代码= -1011请求失败:内部服务器错误(500)" UserInfo = {NSUnderlyingError = 0x1481b9030 {Error Domain = com.alamofire.error.serialization.响应代码= -1016请求失败:不可接受的内容类型:text/html"

Error: Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: internal server error (500)" UserInfo={NSUnderlyingError=0x1481b9030 {Error Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html"

@ Heroku上的错误日志

@Error log on Heroku

[1m [36m用户负载(2.0ms)[0m [1m选择用户".*从用户"订购,按用户"."id" ASC LIMIT 1 [0m 2016-04-30T07:48:18.909397 + 00:00 app [web.1]:在4毫秒内完成了500个内部服务器错误(ActiveRecord:2.0毫秒) 2016-04-30T07:48:18.910250 + 00:00 app [web.1]:ActiveRecord :: ConfigurationError(在Reason上找不到名为"reason"的协会;也许您拼错了吗?):

[1m[36mUser Load (2.0ms)[0m [1mSELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1[0m 2016-04-30T07:48:18.909397+00:00 app[web.1]: Completed 500 Internal Server Error in 4ms (ActiveRecord: 2.0ms) 2016-04-30T07:48:18.910250+00:00 app[web.1]: ActiveRecord::ConfigurationError (Association named 'reason' was not found on Reason; perhaps you misspelled it?):

推荐答案

首先,您以一种非常随意的方式引用reasons. User has_many reasonsprojects,那么为什么不走那条路线呢?

First, you're referencing reasons in a very haphazard way. User has_many reasons through projects, so why not go that route?

current_user.joins(:reasons).where(reasons.updated_at > ?", updated_at)

其次,并且更具体地针对您的错误:您的关系定义has_many :summaries, through: :projects, source: :reasons似乎已损坏,因为projects没有任何summaries.

Secondly, and more specific to your error: your relation definition has_many :summaries, through: :projects, source: :reasons seems to be broken since projects don't have any summaries.

请考虑在您的Project模型中添加has_many :summaries, through: :reasons,然后在User中使用has_many :summaries, through: :projects.

Consider adding a has_many :summaries, through: :reasons to your Project model, then use has_many :summaries, through: :projects in User.

这篇关于Ruby on Rails中具有嵌套关系的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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