【问题标题】:ActiveRecord::Relation code displaying instead of intended dataActiveRecord::关系代码显示而不是预期数据
【发布时间】:2013-03-31 21:41:42
【问题描述】:

我正在尝试在我的主页上显示评论列表,同时显示每条评论的评分。

我想要每个评论附加的不同评级的逗号分隔列表(基于关联的 review_id);评分是 -5 到 5 的整数。加载页面时我得到的只是:

Review 1. #<ActiveRecord::Relation:0x007f9879749a50>
Review 2. #<ActiveRecord::Relation:0x007f9879749a50>
Review 3. #<ActiveRecord::Relation:0x007f9879749a50>
Review 4. #<ActiveRecord::Relation:0x007f9879749a50>
Review 5. #<ActiveRecord::Relation:0x007f9879749a50>

当我想看到这个时:

Review 1. 1, 5, 2, -3
Review 2. 2, -1
Review 3. 1, 1, 4, 5
Review 4. -5, -2, -3
Review 5. 1, 1, 3, 2, 1, 5, 3, 2

用户模型:

class User < ActiveRecord::Base
  attr_accessible :name, :email, :password, :password_confirmation, :remember_me, :ratings_attributes, :reviews_attributes

  has_many :reviews, dependent: :destroy
  has_many :ratings, :through => :reviews, dependent: :destroy
  accepts_nested_attributes_for :ratings, allow_destroy: true
  accepts_nested_attributes_for :reviews, allow_destroy: true
end

审查模型:

class Review < ActiveRecord::Base
  attr_accessible :content, :priority, :ratings_attributes
  belongs_to :user
  has_many :ratings
end

评级模型:

class Rating < ActiveRecord::Base
  attr_accessible :rating_count, :review_id, :timestamp, :user_id

  belongs_to :user
  belongs_to :review
end

家庭控制器:

class HomeController < ApplicationController

  def index
    @reviews = current_user.reviews.order("position")
    @ratings = Rating.where(params[:review_id])
    @ratings_list = Rating.find(:all)
  end

index.html.erb

<h1>Listing reviews</h1>
<p id="notice"><%= notice %></p>
<br />
  <div class="span12">

<ul id="reviews" data-update-url="<%= sort_reviews_url %>">
    <% @reviews.each do |review| %>
        <%= content_tag_for :li, review do %>
            <span class="handle">[drag]</span>
            <%= link_to h(review.content), review %>

            <!-- this is where i want a comma separated list of the different ratings attached to each review; ratings are integers of -5 to 5. -->
            <%= @ratings %>

        <% end %>
    <% end %>
</ul>

<br />
  </div>
<!-- for debugging that i'm actually getting through to the ratings data -->
<%= @ratings_list %>
<br />

<%= link_to 'New Review', new_review_path %>

对于我的一生,我无法弄清楚我是在控制器中还是在 html 中做错了什么。

每条评论旁边显示的都是这样的:

#<ActiveRecord::Relation:0x007f987a9d7618>

提前谢谢...如果你关心的话,我在外面工作,一个虫子飞到我的鼻子上。

【问题讨论】:

  • index.html.erb 上的变量@reviews 来自哪里?
  • 创建此问题时出现复制/粘贴错误。在我的应用程序的主控制器中,它被列为@reviews - 感谢指针 - 不是应用程序中的问题,因为我什至看不到评论

标签: ruby-on-rails activerecord controller associations nested-attributes


【解决方案1】:

您可能正在呈现实际的评论对象,而不是它们的字符串表示形式。像这样的东西应该在视图中起作用:

<%= @ratings.map { |rating| rating.rating_count }.join(", ") %>

这会将您的评分集合转换为 rating_counts 的集合,然后用逗号将它们连接起来。

但我无法帮助解决这个问题:)

【讨论】:

  • 虽然看你的模型结构,但我认为你实际上并不打算渲染@ratings,而是每条评论的评分集合?如果是这样,您可以将我的代码中的@ratings 更改为review.ratings
  • 太棒了。谢谢...工作得很好。我会将此标记为答案,但您是否介意给我指一本书或很好的资源,我可以自己了解地图功能?我已经准备好从初学者过渡到中级,并且大多数教程在初学者时都会下降。我想这更像是一种红宝石资源。
  • Why's (Poignant) Guide to Ruby 是一本非常有趣的初学者和中级书籍。我不记得是否涵盖了 map 方法,但正是这种类型的书让您以正确的心态去寻找这种方法。
  • 浏览 Enumerable module 中的可用方法将帮助您记住 Ruby 可以做的所有方便的小事情。
  • 非常感谢...现在订购这本书并将可枚举的模块内容添加到我的 Kindle。
【解决方案2】:

查询在 Rails 中延迟执行。因此,当您执行Object.where(...) 之类的操作时,它会返回关系对象(如您所见)。它只会在您实际使用它时执行(即循环或调用.all(尽管我相信.all 在rails 4 中略有变化))。要获得逗号分隔的列表,您只需执行&lt;%= @ratings.map(&amp;:rating_count).join(', ') %&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-20
    • 1970-01-01
    • 2014-06-18
    • 2018-03-11
    • 2014-01-11
    • 1970-01-01
    • 2020-02-25
    相关资源
    最近更新 更多