I am creating an app for Rails.How do I sort columns by minimum → maximum in ActiveRecord?

Asked 1 years ago, Updated 1 years ago, 131 views

I am currently creating an app for Rails."Below, I would like to sort by ""sign column minimum value → maximum value"" in the costs table, what should I do?"

The reason why I wanted to sort by "sign column minimum → maximum" is because I use Gem cocoon in Rails, but after registering with form, the order of registration in show.html.erb is different from the order of registration with form.

·Environment
Rails 4.2.3

·Gem
cocoon https://github.com/nathanvda/cocoon
http://rails.densan-labs.net/form/relation_register_form.html

Press the Add Line button in [subjects/_form.html.erb] to add the contents of the partial file (_cost_fields.html.erb).

sign column data types:string

 id:125,
quality: "1",
product: "Product B",
created_at:Tue, 13 Sep 2016 15:53:42 JST+09:00,
updated_at:Tue, 13 Sep 2016 15:53:42 JST+09:00,
sign: "2",
subject_id:93

id: 125,
quality: "3",
product: "Product A",
created_at:Tue, 13 Sep 2016 15:53:42 JST+09:00,
updated_at:Tue, 13 Sep 2016 15:53:42 JST+09:00,
sign: "1",
subject_id:93

id: 125,
quality: "5",
product: "Product C",
created_at:Tue, 13 Sep 2016 15:53:42 JST+09:00,
updated_at:Tue, 13 Sep 2016 15:53:42 JST+09:00,
sign: "3",
subject_id:93

After sorting, I would like to display it in the following order in the view (minimum value in the sign column → maximum value):

 id:125,
quality: "3",
product: "Product A",
created_at:Tue, 13 Sep 2016 15:53:42 JST+09:00,
updated_at:Tue, 13 Sep 2016 15:53:42 JST+09:00,
sign: "1",
subject_id:93

id: 125,
quality: "1",
product: "Product B",
created_at:Tue, 13 Sep 2016 15:53:42 JST+09:00,
updated_at:Tue, 13 Sep 2016 15:53:42 JST+09:00,
sign: "2",
subject_id:93

id: 125,
quality: "5",
product: "Product C",
created_at:Tue, 13 Sep 2016 15:53:42 JST+09:00,
updated_at:Tue, 13 Sep 2016 15:53:42 JST+09:00,
sign: "3",
subject_id:93

[subjects_controller.rb]

class SubjectsController <ApplicationController
  before_action: set_subject, only: [:show,:edit,:update,:destroy]

  # GET/subjects/1
  # GET/subjects/1.json
  def show
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    default_subject
      @subject=Subject.find (params[:id])
    end

  end

end

[subject.rb]

class Subject <ActiveRecord::Base
  has_many:costs,dependent::destroy
  accepts_nested_attributes_for:costs, allow_destroy:true     
end

[cost.rb]

class Cost<ActiveRecord::Base
  belongs_to —subject
end

[subjects/_form.html.erb]

<%=form_for(@subject)do|f|%>
  <%=f.error_messages!%>
  <table>
    <tr>
      <td class="customer_right_space">
        <div class="field form-group">
          <%=f.label:Customer %><br>
          <%=f.text_field:customer,class:'form-control',placeholder:'Sample Corporation'%>
        </div>
      </td>
      <td>
        <div class="field form-group">
          <%=f.label: Subject %><br>
          <%=f.text_field:subject,class:'form-control',placeholder:'Sample Corporation SNO.720'%>
        </div>
      </td>
    </tr>
  </table>
    <%=f.fields_for: costs do | costs_form | %>
      <%=render 'cost_fields', f:costs_form%>
    <%end%>
    <div class="text-center">
      <%=link_to_add_association 'Add line', f, :costs%>
    </div>
    <div class="actions text-center">
      <%=f.submit 'registration', class: 'btn btn-default btn-success visited_white'%>
    </div>
<%end%>

[subjects/_cost_fields.html.erb] *This is what is added when you click the Add Detail button above.

<div class="nested-fields">
      <table class="table table-border">
        <tr class="success">
          <th class="text-center"> sign </th>
          <th colspan="3" class="text-center"> Material/Product Name</th>
          <th class="text-center">Pressure</th>
          <th class="text-center">Size</th>
          <th colspan="3" class="text-center">Notes</th>
        </tr>
        <tr>
          <td rspan="3" class="vertical_center">%=f.text_field:sign, class:'form-control'%>>/td>>!---sign -->
          <td colspan="3" class="field form-group">%=f.text_area:product, class:'form-control', placeholder:'Product A'%>>/td><!--Materials and Product Name -->
          <td class="field form-group">%=f.text_field:pressure, class:'form-control'%>/td><!--Pressure -->
          <td class="field form-group">%=f.text_field:size, class:'form-control'%>/td><!---size-->
          <td colspan="3" class="field form-group">%=f.text_area:remarks, placeholder:"Notes", class:'form-control'%></td><!--Notes-->
        </tr>
        <tr>
          <th class="text-center"> quantity </th>
          <th class="text-center">HVK</th>
          <th class="text-center">Fujii</th>
          <th class="text-center">Imamoto</th>
          <th class="text-center">cost</th>
          <th class="text-center"> Original Total </th>
          <th class="text-center">Sales Price</th>
          <th class="text-center">Sales meter </th>
        </tr>
        <tr class="up_area">
          <td class="field form-group">%=f.text_field:quantity, id:"num01", class:'form-control num'%>>/td><!--- Qty -->
          <td class="field form-group">%=f.text_field:hvk, id:"num02", class:'form-control hvk num'%>>/td><!--HVK-->
          <td class="field form-group">%=f.text_field:fujii, id:"num03", class:'form-control fujiinum'%>>/td>>!--Fujii-->
          <td class="field form-group">%=f.text_field:imamoto, id:"num04", class:'form-control imamoto num'%>>/td>>!--Imamoto -->
          <td class="field form-group">%=f.text_field:unit_price, id:"ans01", class:'form-control unit_price'%>>/td><!--cost -->
          <td class="field form-group">%=f.text_field:price_total,id:"ans02", class:'form-control price_total'%></td><!--- Original Total -->
          <td class="field form-group">%=f.text_field:selling_price, id:"num05", class:'form-control selling_price_total num'%>>/td>!--Sales price -->
          <td class="field form-group">%=f.text_field:selling_price_total, id:"ans03", class:'form-control selling_price_total'%>>/td><!--- Total sales -->
        </tr>
        <tr>
          <td><%=link_to_remove_association 'delete', f, class: 'btn btn-default'%>/td>
        </tr>
      </table>
    </div>

[subjects/show.html.erb]

<table>
 <tr>
   <th> Signs </th>
   <th> Quantity </th>
   <th>Products</th>
   <th>Pressure </th>
   <th>Size</th>
 </tr>

<%@subject.costs.each do | cost | %> 
 <tr>
   <td><%=cost.sign%>/td>
   <td><%=cost.quantity%>/td>
   <td><%=cost.product%>/td>
   <td><%=cost.pressure%>/td>
   <td><%=cost.size%>/td>
 </tr>
<%end%>
</table>

<%=link_to 'Edit', edit_subject_path(@subject)%>
<%=link_to 'Destroy', @subject,method::delete,data:{confirm:'Are you sure?'}%>
<%=link_to 'Back', subjects_path%>

ruby-on-rails ruby rails-activerecord

2022-09-30 21:18

1 Answers

It is not directly related to the question, but use Strong Parameters instead of using params directly when appropriate.

You can sort by .costs.order(:sign) and sign where appropriate.

If you are comfortable calling order in View,
<%@subject.costs.order(:sign).each do|cost|%>
should solve this problem (I think it would be better to refactor it so that the controller can call it properly).It is also not directly related to order, but probably N+1 problems with the current source.)

I think you might want to call .includes(:cost).order('costs.sign') in the appropriate place instead of costs.order(:sign) (probably this way will eliminate the N+1 problem and you can call order on the controller).
I can't try it here, but I think it will look like Subject.find.includes(:cost).order('costs.sign').


2022-09-30 21:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.