Joe Edgar
Joe Edgar

Reputation: 940

Rails ActiveRecord - Updating polymorphic id

I have an app where I have three models. I have leads and customers which are very similar (but different enough that they warranted their own models). Then I have a polymorphic model called "Interactions" which has an interactionable_id and an interactionable_type (which would, of course, be either 'customer' or 'lead').

I recently tried to build a method that would migrate an interaction from a lead to a customer (when a lead is upgraded to being a customer).

I wanted to keep my code lean, and in order to keep my method simple, I decided to simply update the attributes of the interaction so that the interactionable_id and interactionable_type would change to the customer's id and to "Customer" respectively.

I tried many various methods of doing this, and the only thing that I could get to update was the interactionable_type. The interactionable_id would not change, no matter what I tried. I tried putting in a static number (rather than customer.id). I tried update_attributes and I tried to update the attributes individually and then save it. Also, I did have :interactionable_id in the attr_accessible list.

I got this to work because I decided to approach the problem slightly differently, but I could not find any answers out there as to exactly why this was happening.

I was just wondering if anyone knew why this might be. Is there some sort of a lock on polymorphic association ids? Why couldn't I change the association?

Upvotes: 0

Views: 250

Answers (1)

user946611
user946611

Reputation:

This should work

interaction.interactionable = customer
interaction.save

Upvotes: 1

Related Questions