0

我的模型中有以下方法:

  def self.set_bad_recommedation_size(rating_set)
    bad = Rating.where(rating_set: rating_set).where(label: 'Bad').count
    total = Rating.where(rating_set: rating_set).count
    percentage_bad = (bad.to_f/total.to_f * 100)
    return bad, total, percentage_bad
  end

在我看来,如何调用变量 bad、total、percentage_bad。我想要的是:

<%= "#{Model.set_bad_recommedation_size(rating_set).bad}/#{Model.set_bad_recommedation_size(rating_set).total"%>
4

2 回答 2

1

你最好这样做:

<% bad, total, percentage_bad = Model.set_bad_recommedation_size(rating_set) %>
<%= "#{bad}/#{total}" %>

这样你就不会多次调用该方法。

于 2013-03-26T02:33:47.560 回答
1

我会添加一个中间助手,以便您的视图更好地阅读

<%= bad_recommendation_ratio(result_set) %>

应用程序.helper

def bad_recommendation_ratio(result_set)
  bad, total = Model.set_bad_recommedation_size(rating_set)
  "#{bad}/#{total}"
end
于 2013-03-26T02:41:56.163 回答