Rails 方法在视图中不可用

发布于 2024-10-14 06:50:50 字数 1094 浏览 0 评论 0原文

我收到这个奇怪的错误。我为我的模型定义了一个方法

    class Rating < ActiveRecord::Base

  [...]

  belongs_to :user
  belongs_to :movie

  [...]

  def to_text
    texto = case self.grade
            when 0..1 then "horrible"
            when 2..3 then "bad"
            when 4..5 then "not bad"
            when 6..7 then "good"
            when 8..9 then "very good"
            when 10 then "master piece"
            end
  end
end

然后,在我的控制器上,我定义了这个实例:

@[email protected](:user_id => current_user)

它确实找到了它,所以它可以工作。但是,当我调用该方法或类似的属性时,

<%= @current_user_rating.grade %>
<%= @current_user_rating.to_text %>

我会收到此错误

undefined method `grade' for []:ActiveRecord::Relation
undefined method `to_text' for []:ActiveRecord::Relation

为什么变量不表现为具有适当属性和方法的实例,而是表现为关系?

它确实可以在控制台上运行,但不能在服务器上运行...

I get this strange error. I have defined a method for my model

    class Rating < ActiveRecord::Base

  [...]

  belongs_to :user
  belongs_to :movie

  [...]

  def to_text
    texto = case self.grade
            when 0..1 then "horrible"
            when 2..3 then "bad"
            when 4..5 then "not bad"
            when 6..7 then "good"
            when 8..9 then "very good"
            when 10 then "master piece"
            end
  end
end

Then, on my controller, I define this instance:

@[email protected](:user_id => current_user)

And it does find it, so it works. But then, when I call the method, or a property like

<%= @current_user_rating.grade %>
<%= @current_user_rating.to_text %>

I get this error

undefined method `grade' for []:ActiveRecord::Relation
undefined method `to_text' for []:ActiveRecord::Relation

Why the variable not behaving as an instance with appropriate attributes and methods but as a Relation?

It does work on the Console, but not on the server...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

埋情葬爱 2024-10-21 06:50:50

由于一部电影有多个评级,因此 @current_user_ rating 是它们的集合,即使有一个。您应该能够通过像这样调用您的方法来消除错误:

<% @current_user_rating.each do |rating| %>
  <%= rating.grade %>
  <%= rating.to_text %>
<% end %>

since a movie has multiple ratings, @current_user_rating is a collection of them, even if there is one. you should be able to get rid of the errors by calling your methods like this:

<% @current_user_rating.each do |rating| %>
  <%= rating.grade %>
  <%= rating.to_text %>
<% end %>
心意如水 2024-10-21 06:50:50

当您调用 where 时,它实际上并不查询数据库,而是创建一个可以在数据库上转换和运行的对象。这对于在运行之前进一步修改查询非常有用。

通常会添加对 firstall 的进一步调用以实际执行查询并获取结果。 (请注意,如果您在 Rails 控制台中调用 where ,这可能会自动完成,以便可以打印结果)

在您的情况下,看起来用户对电影的评价不应超过一次,所以以下内容似乎是合适的:

@[email protected](:user_id => current_user).first

编辑:实际上我认为 GSto 是正确的,我上面写的看起来不像是你失败的原因。该错误消息实际上是在抱怨您尝试调用的方法在对象数组上调用无效。在这种情况下,first 只是选择第一个结果并忽略任何其他结果。

最好仔细检查一下是否有单个用户对某些电影有多个评分。事实上,我建议添加一个验证,以确保如果不是有意的,这是不允许的。

When you call where it doesn't actually query the database, it creates an object which can be converted and run on the database. This is useful for further modifying the query before it is run.

Typically a further call to first or all is added to actually execute the query and get the result. (Note that this might be done automatically if you call where in the rails console, so that the result can be printed)

In your case, it looks like a user is expected to rate a movie no more than once, so the following seems appropriate:

@[email protected](:user_id => current_user).first

EDIT: Actually I think GSto is correct, what I wrote above does not look like the reason for your failure. The error message is actually complaining that the methods you are trying to call aren't valid to be called on an Array of objects. In this case, first is simply selecting the first result and ignoring any others.

It would be good to double-check if you have any movies for which a single user has multiple ratings. In fact, I'd recommend adding a validation to ensure that this is not allowed, if it is not intended.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文