generated from ita-social-projects/DevTemplate
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add uniqueness for product and validation in model, change all auplac…
…ated products if exists with migration
- Loading branch information
1 parent
f8acd86
commit 1a95a02
Showing
5 changed files
with
27 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class AddIndexToProduct < ActiveRecord::Migration[6.1] | ||
def up | ||
duplicated_titles = Product | ||
.select(:title) | ||
.group(:title) | ||
.having('count(title) > 1') | ||
.pluck(:title) | ||
|
||
duplicated_titles.each do |dup_title| | ||
products = Product.where(title: dup_title) | ||
products.map { |product| product.update(title: "#{product.title}_#{product.uuid}")} | ||
end | ||
|
||
add_index :products, :title, unique: true | ||
end | ||
|
||
def down | ||
remove_index :products, :title | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters