Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>def update @folder = Folder.find(params[:id]) @folder.attributes = params[:folder] add_new_file = false delete_file = false @folder.files.each do |file| add_new_file = true if file.new_record? delete_file = true if file.marked_for_destruction? end both = add_new_file &amp;&amp; delete_file if both redirect_to "both_action" elsif add_new_file redirect_to "add_new_file_action" elsif delete_file redirect_to "delete_file_action" else redirect_to "folder_not_changed_action" end end </code></pre> <p>Sometimes you want to know that folder is changed without determining how. In that case you can use <code>autosave</code> mode in your association:</p> <pre><code>class Folder &lt; ActiveRecord::Base has_many :files, :autosave =&gt; true accepts_nested_attributes_for :files attr_accessible :files_attributes end </code></pre> <p>Then in controller you can use <code>@folder.changed_for_autosave?</code> which returns whether or not this record has been changed in any way (new_record?, marked_for_destruction?, changed?), including whether any of its nested autosave associations are likewise changed.</p> <p><strong>Updated.</strong></p> <p>You can move model specific logic from controller to a method in <code>folder</code> model, e.q. <code>@folder.how_changed?</code>, which can return one of :add_new_file, :delete_file and etc. symbols (I agree with you that it's a better practice, I'd just tried to keep things simple). Then in controller you can keep logic pretty simple. </p> <pre><code>case @folder.how_changed? when :both redirect_to "both_action" when :add_new_file redirect_to "add_new_file_action" when :delete_file redirect_to "delete_file_action" else redirect_to "folder_not_changed_action" end </code></pre> <p>This solution uses 2 methods: <code>new_record?</code> and <code>marked_for_destruction?</code> on each child model, because Rails <em>in-box</em> method <code>changed_for_autosave?</code> can tell only that children were changed without how. This is just the way how to use this indicators to achieve your goal.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload