Question

I am trying to accomplish the following inside Spree migrations:

ActiveRecord::Base.connection.tables.each do |table|
    table.constantize.update_all(user_id: 1)
end

:user_id is present in all the tables present in the database.

I want to update the :user_id column in all the tables to 1.

"spree_products".constantize 

isn't returning "Spree::Product"

I have used the following code successfully for updating the user_ids:

ActiveRecord::Base.connection.tables.each do |table|
    Spree.const_get(table.gsub("spree_", "").classify).update_all(user_id: 1)
end
Was it helpful?

Solution

You can gsub _ with / and use classify

> "spree_products".gsub('_', '/').classify
# => "Spree::Product"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top