简体   繁体   中英

How to Order Nested Attribute according to its Date?

How can we DESC order results according to its :date_value in the quantifieds index ?

Results being the nested attribute to quantifieds.

Right now the order is according to where the User added the result in the form , regardless of :date_value .

This has proven more difficult than I would have guessed.

 class QuantifiedsController < ApplicationController before_action :set_quantified, only: [:show, :edit, :update, :destroy] before_action :logged_in_user, only: [:create, :destroy] def index if params[:tag] @quantifieds = Quantified.tagged_with(params[:tag]) else @quantifieds = Quantified.joins(:results).all @averaged_quantifieds = current_user.quantifieds.averaged @instance_quantifieds = current_user.quantifieds.instance end end def show end def new @quantified = current_user.quantifieds.build end def edit end def create @quantified = current_user.quantifieds.build(quantified_params) if @quantified.save redirect_to quantifieds_url, notice: 'Quantified was successfully created' else @feed_items = [] render 'pages/home' end end def update if @quantified.update(quantified_params) redirect_to quantifieds_url, notice: 'Goal was successfully updated' else render action: 'edit' end end def destroy @quantified.destroy redirect_to quantifieds_url end private def set_quantified @quantified = Quantified.find(params[:id]) end def correct_user @quantified = current_user.quantifieds.find_by(id: params[:id]) redirect_to quantifieds_path, notice: "Not authorized to edit this goal" if @quantified.nil? end def quantified_params params.require(:quantified).permit(:categories, :metric, :result, :date, :tag_list, results_attributes: [:id, :result_value, :date_value, :_destroy]) 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 Result < ActiveRecord::Base belongs_to :user belongs_to :quantified end 

 class CreateQuantifieds < ActiveRecord::Migration def change create_table :quantifieds do |t| t.string :categories t.string :metric t.references :user, index: true t.timestamps null: false end add_foreign_key :quantifieds, :users add_index :quantifieds, [:user_id, :created_at] end end 

 class CreateResults < ActiveRecord::Migration def change create_table :results do |t| t.string :result_value t.date :date_value t.integer :quantified_id t.timestamps null: false end end end 

form

 <%= javascript_include_tag "quantified.js" %> <%= simple_form_for(@quantified) do |f| %> <%= f.error_notification %> <div class="america"> <form> <% Quantified::CATEGORIES.each do |c| %>&nbsp; <%= f.radio_button(:categories, c, :class => "date-format-switcher") %>&nbsp; <%= label(c, c) %> <% end %> <br/> <br/> <div class="form-group"> <%= f.text_field :tag_list, quantified: @quantified.tag_list.to_s.titleize, class: 'form-control', placeholder: 'Enter Action' %> </div> <div class="form-group"> <%= f.text_field :metric, class: 'form-control', placeholder: 'Enter Metric' %> </div> <div id="results"> <%= f.fields_for :results do |result| %> <%= render 'result_fields', :f => result %> <% end %> </div> <div class="links"> &nbsp;&nbsp;<b><%= link_to_add_association 'Add Result', f, :results %></b> </div> <div class="america2"> <%= button_tag(type: 'submit', class: "btn") do %> <span class="glyphicon glyphicon-plus"></span> <% end %> <%= link_to quantifieds_path, class: 'btn' do %> <span class="glyphicon glyphicon-chevron-left"></span> <% end %> <%= link_to @quantified, method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn' do %> <span class="glyphicon glyphicon-trash"></span> <% end %> </div> </form> </div> <% end %> 

index

 <!-- Default bootstrap panel contents --> <div id="valuations" class="panel panel-default"> <div class="panel-heading"><h4><b>AVERAGE</b></h4></div> <% @averaged_quantifieds.each do |averaged| %> <div class="attempt"> <b><%= raw averaged.tag_list.map { |t| link_to t.titleize, tagquantifieds_path(t) }.join(', ') %> <%= link_to edit_quantified_path(averaged) do %> (<%= averaged.metric %>)</b> <% end %> <ul> <% averaged.results.each do |result| %> <li> <b><%= result.result_value %></b>&nbsp;&nbsp; <%= result.date_value.strftime("%b %Y") %> </li> <% end %> </ul> </div> <% end %> </div> <div class="valuations-button"> <%= link_to new_quantified_path, class: 'btn' do %> <b><span class="glyphicon glyphicon-plus"</span></b> <% end %> </div> <br> <!-- Default bootstrap panel contents --> <div id="valuations" class="panel panel-default"> <div class="panel-heading"><h4><b>INSTANCE</b></h4></div> <% @instance_quantifieds.each do |instance| %> <div class="attempt"> <b><%= raw instance.tag_list.map { |t| link_to t.titleize, tagquantifieds_path(t) }.join(', ') %> <%= link_to edit_quantified_path(instance) do %> (<%= instance.metric %>)</b> <% end %> <ul> <% instance.results.each do |result| %> <li> <%= result.date_value.strftime("%b.%d.%y") %> &nbsp;&nbsp; <%= result.result_value %> </li> <% end %> </ul> </div> <% end %> </div> <div class="valuations-button"> <%= link_to new_quantified_path, class: 'btn' do %> <b><span class="glyphicon glyphicon-plus"</span></b> <% end %> </div> 

Thanks so much for your time!

Got it! Add default_scope { order('date_value DESC') } in result.rb

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM