Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>After a week of experimentation I just thought I'd post what I finally did:</p> <p>I did in fact split the photo into two models, because I ended up having to create empty records with any approach I tried. I found it was easier in the end to have two seperate models because:</p> <ol> <li><p>It made it easier to work within Rails convention (use the standard REST actions for the second model to handle asynchronous updates, rather than having to add several custom actions to the parent model).</p></li> <li><p>No matter which option I tried I ended up having orphan records as a possibility. I found it was easier to have a parent object which does not ever save unless valid (the Photo model in my case), and to have attachments which may be orphans. The attachments are never called directly anywhere in the app, so there's no risk of accidentally having an empty record being pulled up, and no need to set a default scope or something in order to only show "valid" photos. Cleaning up orphans is pretty easy, just do <code>Attachment.where( :parent_id =&gt; nil )</code> and delete all those.</p></li> <li><p>I can maintain the previous code by simply delegating attachment to photo. See <a href="https://stackoverflow.com/questions/4642105/what-is-a-more-ruby-like-way-of-doing-this-command/4642741#4642741">this answer</a> on another question.</p></li> </ol> <p>I hope this saves others some trouble down the road. If you need Ajax functionality with attachments it is best to make them their own model. Also, for adding ajax file upload to forms, check out <a href="https://github.com/formasfunction/remotipart" rel="nofollow noreferrer">https://github.com/formasfunction/remotipart</a>. This saved my app.</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. 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