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
有帮助吗?

解决方案

You can gsub _ with / and use classify

> "spree_products".gsub('_', '/').classify
# => "Spree::Product"
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top