Search code examples
ruby-on-railsruby-on-rails-3ruby-on-rails-3.1

Returning multilpe values from model method in view


I have the following method in my model:

  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

How do I call the variable bad, total, percentage_bad in my view. What I want:

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

Solution

  • You're better off doing:

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

    That way you're not calling the method multiple times.