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?

Reply

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| %>

Reply
Join the discussion
Create an account Log in

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

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

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

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2024 GoRails, LLC. All rights reserved.