简体   繁体   English

来自迭代器块中关联模型的访问信息

[英]Access Information from an associated model in an iterator block

On a user profile page, I want to show the "reviews" that that user has as well as "review requests". 在用户个人资料页面上,我想显示该用户拥有的“评论”以及“评论请求”。 I currently have it set up where listing review requests works, like this: 我目前已在列表审核请求的工作位置进行设置,如下所示:

  def show
    @user = User.find(params[:id])
    @review_requests = @user.review_requests.paginate(page: params[:page])
  end

and the view: 和视图:

    <ol class="review_request">
      <%= render 'user_review_requests' %>
    </ol>

with partial: 部分:

  <% @review_requests.each do |review_request| %>
  <li>
    <span class="description">
      <%= review_request.description %>
    </span>
   blah blah blah....(there's more of much the same)

@reviews = @user.reviews.paginate(page: params[:page])

I also want to show reviews, but the thing is, a lot of the information about the review I want to show is actually stored in the ReviewRequest model. 我也想显示评论,但事实是,有关我要显示的评论的许多信息实际上存储在ReviewRequest模型中。 So doing this wouldn't work: 所以这样做是行不通的:

add to the show controller: 添加到显示控制器:

@reviews = @user.reviews.paginate(page: params[:page])

and the iterator block: 和迭代器块:

<% @reviews.each do |review| %>
  <li>
    <span class="description">
      <%= review.review_request_id.description %>
    </span>

How can I access information from the ReviewRequest model that is associated with each connected review so I can show it on the profile page. 如何从与每个已连接评论关联的ReviewRequest模型中访问信息,以便可以在个人资料页面上显示它。

BTW - review requests have_many reviews and reviews belong_to review requests 顺便说一句-审核请求has_many多个评论和属下的_以审核请求

When accessing a belongs_to association, you access it via the association name ( review_request ), not the column name ( review_request_id ). 当访问一个belongs_to关联时,您可以通过关联名称( review_request )而不是列名称( review_request_id )进行访问。 Something like this: 像这样:

<span class="description">
  <%= review.review_request.description %>
</span>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM