Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use the new nested attributes in Rails 2.3, but there is a certain way you have to write it to make it work. The trick is that you need to create the actual polymorphic object, then build the class that has the belongs to polymorphic clause in it. This is an example I found <a href="http://ryandaigle.com/articles/2009/2/1/what-s-new-in-edge-rails-nested-attributes" rel="nofollow noreferrer">at Ryans Scraps</a>, posted by a user named: Superslau (I've cleaned it up a good bit for here):</p> <blockquote> <p>This feature is really awesome. I have implemented this with polymorphic associations, and it works!</p> </blockquote> <pre><code> class Task &lt; ActiveRecord::Base has_many :assets, :dependent=&gt;:destroy accepts_nested_attributes_for :assets, :allow_destroy =&gt; true belongs_to :workable, :polymorphic =&gt; true end class Upload &lt; ActiveRecord::Base has_one :task, :as =&gt; :workable, :dependent=&gt;:destroy accepts_nested_attributes_for :task, :allow_destroy =&gt; true end </code></pre> <blockquote> <p>Upload is a kind of task. All tasks can have one or more assets uploaded.</p> <p>I took me a while to figure out that I should use the Upload model as the parent. So in one form, I can create an upload, and it’s corresponding task entry, along with a file upload.</p> <p>in my controller:</p> </blockquote> <pre><code> def new @upload = Upload.new @upload.task = Task.new @upload.task.assets.build end </code></pre> <blockquote> <p>Don’t worry if that doesn’t make any sense, I just wanted to let people know that accepts_nested_attributes_for works just fine with polymorphic associations. Thanks Eloy!</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    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.
    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