如何使嵌套模型范围在侧栏中工作?

时间:2015-03-05 22:51:04

标签: ruby-on-rails ruby model-view-controller scope sidebar

我们如何在侧边栏中使 layouts / _stats.html.erb 工作,并在所有页面上显示:

<b><center>Average</center></b>

<ul>
<li>
  <% @averaged_quantifieds.each do |averaged| %>
  <% averaged.statsresults.each do |result| %>
        <%= result.date_value.strftime("%b") %>:&nbsp;&nbsp;
        <%= link_to edit_quantified_path(averaged) do %>
        <b><%= raw averaged.tag_list.map { |t| link_to t.titleize, tagquantifieds_path(t) }.join(', ') %></b><% end %>
        <%= result.result_value %>
        <%= averaged.metric %>

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

<b><center>Instance</center></b>

<ul>
  <% @instance_quantifieds.each do |instance| %>
  <% instance.statsresults.each do |result| %>
        <%= result.date_value.strftime("%b %d") %>:&nbsp;&nbsp;
        <%= link_to edit_quantified_path(instance) do %>
        <b><%= raw instance.tag_list.map { |t| link_to t.titleize, tagquantifieds_path(t) }.join(', ') %></b><% end %>
        <%= result.result_value %>
        <%= instance.metric %>
</ul>
<% end %>
<% end %>

未收到此错误:undefined method "statsresults" for #<Quantified:0x007ff739b42728>

生成<% averaged.statsresults.each do |result| %>

结果属于量化,正如您在下面看到的那样,我正在尝试使用范围将潜在的:results数量限制为最新的:< / p>

class Result < ActiveRecord::Base
  belongs_to :user
  belongs_to :quantified

  scope :stats, -> do
	  order("date_value").
	  limit(1)
  end
end

class Quantified < ActiveRecord::Base
	belongs_to :user
 	has_many :results #correct
	accepts_nested_attributes_for :results, :reject_if => :all_blank, :allow_destroy => true #correct
 	scope :averaged,  -> { where(categories: 'Averaged') }
 	scope :instance,  -> { where(categories: 'Instance') }
 	validates :categories, :metric, presence: true
	acts_as_taggable

	CATEGORIES = ['Averaged', 'Instance']
end

class ApplicationController < ActionController::Base
  before_action :load_todays_habits
  before_action :set_top_3_goals
  before_action :randomize_value
  before_action :set_stats
  protect_from_forgery with: :exception
  include SessionsHelper

  def set_top_3_goals
    @top_3_goals = current_user.goals.unaccomplished.top_3 if current_user
  end

  def randomize_value
    @sidebarvaluations = current_user.valuations.randomize if current_user
  end

  def set_stats
    @quantifieds = Quantified.joins(:results).all
    @averaged_quantifieds = current_user.quantifieds.averaged
    @instance_quantifieds = current_user.quantifieds.instance
    @statsresults = current_user.results.stats
  end

感谢您的帮助,如果您有时间,我还可以使用其他范围问题的帮助:https://stackoverflow.com/questions/28863999/how-to-automate-into-method

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

<b><center>Average</center></b>

<ul>
  <% @averaged_quantifieds.each do |averaged| %>
    <li>
      <% averaged.results.each do |result| %>
        <%= result.date_value.strftime("%b") %>:&nbsp;&nbsp;
        <%= link_to edit_quantified_path(averaged) do %>
          <b><%= raw averaged.tag_list.map { |t| link_to t.titleize, tagquantifieds_path(t) }.join(', ') %></b>
        <% end %>
        <%= result.result_value %>
        <%= result.metric %>
      <% end %>
    </li>
  <% end %>
</ul>

如果这得到了预期的结果,您可以对@instanced_quantifieds

执行相同的操作