Ruby on Rails where query with nesting relations

﹥>﹥吖頭↗ 提交于 2019-12-23 02:43:17

问题


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

Ruby on Rails where query with relations

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

Model

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

  # 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

  # 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  

@Error log on iOS

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"

@Error log on Heroku

[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?):

|improve this question

回答1:


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)

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.

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



来源:https://stackoverflow.com/questions/36952179/ruby-on-rails-where-query-with-nesting-relations

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!