質問

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