Note that there are some explanatory texts on larger screens.

plurals
  1. POHas_many/belongs_to association not saving due to polymorphic associations?
    primarykey
    data
    text
    <p>I'm trying to create a log of attributes that have changed within each update to a certain message. The attributes are <code>IssueStatus</code> and <code>IssueType</code>, and both have two polymorphic associations with <code>UpdateAction</code>, that saves the <code>changed_from_id</code> and <code>changed_from_type</code> as well as <code>changed_to_id</code> and <code>changed_to_type</code> perfectly. </p> <pre><code>class IssueStatus &lt; ActiveRecord::Base has_many :update_actions, :as =&gt; :changed_to has_many :update_actions, :as =&gt; :changed_from end class IssueType &lt; ActiveRecord::Base has_many :update_actions, :as =&gt; :changed_to has_many :update_actions, :as =&gt; :changed_from end class Update &lt; ActiveRecord::Base has_many :update_actions end class UpdateAction &lt; ActiveRecord::Base belongs_to :changed_to, :polymorphic =&gt; true belongs_to :changed_from, :polymorphic =&gt; true belongs_to :update end </code></pre> <p>The problem is that trying to save a <code>UpdateAction</code> to <code>Update.update_actions</code> doesn't work. Rails returns <code>true</code> for both <code>save</code> and <code>save!</code>, but my development.log shows no SQL queries being performed. I cannot find out what I'm doing wrong, as nothing is logged (or I don't know how to find it). Working from the console:</p> <pre><code>&gt;&gt; action = UpdateAction.new =&gt; #&lt;UpdateAction id: nil, changed_from_id: nil, changed_from_type: nil, changed_to_id: nil, changed_to_type: nil, update_id: nil, created_at: nil, updated_at: nil&gt; &gt;&gt; action.changed_from = from =&gt; #&lt;IssueStatus id: 2, name: "Open", created_at: "2009-12-02 05:34:41", updated_at: "2009-12-02 05:34:41"&gt; &gt;&gt; action.changed_to = to =&gt; #&lt;IssueStatus id: 1, name: "Closed", created_at: "2009-12-02 05:34:30", updated_at: "2009-12-02 05:34:30"&gt; &gt;&gt; action.save =&gt; true &gt;&gt; update = Update.last =&gt; #&lt;Update id: 1, description: "Yawn", created_at: "2009-12-02 05:19:25", updated_at: "2009-12-02 05:19:25"&gt; &gt;&gt; u.update_actions &lt;&lt; action =&gt; [#&lt;UpdateAction id: 2, changed_from_id: 2, changed_from_type: "IssueStatus", changed_to_id: 1, changed_to_type: "IssueStatus", update_id: 1, created_at: "2009-12-02 05:35:16", updated_at: "2009-12-02 05:35:16"&gt;] &gt;&gt; u.save =&gt; true &gt;&gt; u.update_actions =&gt; [#&lt;UpdateAction id: 2, changed_from_id: 2, changed_from_type: "IssueStatus", changed_to_id: 1, changed_to_type: "IssueStatus", update_id: 1, created_at: "2009-12-02 05:35:16", updated_at: "2009-12-02 05:35:16"&gt;] &gt;&gt; u.reload =&gt; #&lt;Update id: 1, description: "Yawn", created_at: "2009-12-02 05:19:25", updated_at: "2009-12-02 05:19:25"&gt; &gt;&gt; u.update_actions =&gt; [] </code></pre> <p>Any help will be greatly appreciated, I guess I'm doing something wrong but I've been looking at it too long to figure out what. Thanks in advance!</p>
    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.
 

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