Search code examples
ruby-on-railsrubyruby-on-rails-4polymorphic-associationsproduction-environment

Updating current posts in production's targetable_id


I have two tables, one of which I changed to have polymorphic associations. I have a production environment with about 40 posts, and I want to change their targetable_id and targetable_types, but I'm having trouble through the CL with:

Design.first.update_attributes(targetable_id: 1, targetable_type: "Collection")
-->false

In this case, the former way to find the design was with a collection_id, and now that's changed to a targetable_id. The Collection ID on this specific design is 1.

How can I do this without deleting all the posts and starting over?


Solution

  • You could try using update_columns which will update the database directly without performing any callbacks or validation:

    Design.first.update_columns(targetable_id: 1, targetable_type: "Collection")

    However, if it were me, I'd try to figure out exactly why it's failing... If you try:

    Design.first.update_attributes!(targetable_id: 1, targetable_type: "Collection")

    ... the console should give you additional information as to why it's failing, which may help you run update_attributes.