All threads / How do I sort fields for and include a new fields for entry?

Ask A Question

Notifications

You’re not receiving notifications from this thread.

How do I sort fields for and include a new fields for entry?

William Jones asked in Rails

Sorry for the confusing question, I couldn't think of a better way to phrase it.

I have a 'Prospect' model that accepts nested attributes for 'prospect_calls'. When editing the prospect I need to see all the prospect calls in the order that they were made and be able to add a new prospect call if required.

In the controller I have the following code,

@prospect.prospect_calls.build call_date: @required_date

This adds a new call record that can be edited or ignored by the user. In the prospect model I reject prospect calls if the "who" field on the call record is not entered. This all works perfectly if the calls are not sorted. If they are sorted the new call record does not appear in the call list on the edit view.

To sort the calls I have included the following in the view,

<%= form.fields_for :prospect_calls, form.object.prospect_calls.order(:call_date) do |p| %>

This sorts the calls perfectly, but the new call record does not appear in the list of calls.

How can I sort the call records and have a new call record on the edit view?

I changed the sort in the view as follows and everything works.

<%= form.fields_for :prospect_calls, form.object.prospect_calls.sort_by(&:call_date) do |p| %>

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 37,344+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.