如何确保 table 不包含重复项(基于两个外键)?
How to make sure that table do not contain duplicates (based on two foreign keys)?
我有三个模型:
class Ingredient < ActiveRecord::Base
has_one :component
end
class Size < ActiveRecord::Base
has_one :component
end
class Component < ActiveRecord::Base
belongs_to :ingredient
belongs_to :size
end
我的 schema.rb
看起来像:
ActiveRecord::Schema.define(version: 20160414202240) do
create_table "components", force: :cascade do |t|
t.boolean "active"
t.decimal "cost"
t.decimal "price"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "size_id"
t.integer "ingredient_id"
end
add_index "components", ["ingredient_id"], name: "index_components_on_ingredient_id"
add_index "components", ["size_id"], name: "index_components_on_size_id"
create_table "ingredients", force: :cascade do |t|
t.string "name"
t.boolean "active"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "sizes", force: :cascade do |t|
t.string "name"
t.boolean "active"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
现在,我正在为数据库播种:
size_small = Size.create(name: 'Small', active: true)
size_medium = Size.create(name: 'Medium', active: true)
size_large = Size.create(name: 'Large', active: true)
ingredient_tomato = Ingredient.create(name: 'Tomato', active: true)
ingredient_onion = Ingredient.create(name: 'Onion', active: true)
ingredient_red_onion = Ingredient.create(name: 'Red onion', active: true)
ingredient_champignons = Ingredient.create(name: 'Champignons', active: true)
ingredient_shrimps = Ingredient.create(name: 'Shrimps', active: true)
Component.create(cost: 0.20, price: 1.00, active: true, size: size_small, ingredient: ingredient_tomato)
Component.create(cost: 0.20, price: 1.00, active: true, size: size_small, ingredient: ingredient_onion)
Component.create(cost: 0.20, price: 1.00, active: true, size: size_small, ingredient: ingredient_red_onion)
Component.create(cost: 0.30, price: 1.00, active: true, size: size_small, ingredient: ingredient_champignons)
Component.create(cost: 0.50, price: 1.50, active: true, size: size_small, ingredient: ingredient_shrimps)
Component.create(cost: 0.30, price: 1.50, active: true, size: size_medium, ingredient: ingredient_tomato)
Component.create(cost: 0.30, price: 1.50, active: true, size: size_medium, ingredient: ingredient_onion)
Component.create(cost: 0.30, price: 1.50, active: true, size: size_medium, ingredient: ingredient_red_onion)
Component.create(cost: 0.45, price: 1.50, active: true, size: size_medium, ingredient: ingredient_champignons)
Component.create(cost: 0.75, price: 2.25, active: true, size: size_medium, ingredient: ingredient_shrimps)
Component.create(cost: 0.40, price: 2.00, active: true, size: size_large, ingredient: ingredient_tomato)
Component.create(cost: 0.40, price: 2.00, active: true, size: size_large, ingredient: ingredient_onion)
Component.create(cost: 0.40, price: 2.00, active: true, size: size_large, ingredient: ingredient_red_onion)
Component.create(cost: 0.60, price: 2.00, active: true, size: size_large, ingredient: ingredient_champignons)
Component.create(cost: 1.00, price: 3.00, active: true, size: size_large, ingredient: ingredient_shrimps)
# This one uses again `size_small` and `ingredient_tomato` and shouldn't be allowed.
Component.create(cost: 2.99, price: 7.99, active: true, size: size_small, ingredient: ingredient_tomato)
什么是最rails-api
验证的方法,size
和 ingredient
组合在一起应该在 Component
table 中是唯一的?
我应该在 Component
的控制器中实现一些逻辑还是可以设置一些规则/范围/其他?
请原谅我的无知,我才刚刚开始学习Ruby(和Rails)。
我正在使用 Rails 4.2.6
和 Ruby 2.3.0p0 (2015-12-25 revision 53290)
。
提前致谢。
您将需要为模型添加范围唯一性验证并为组件添加唯一索引 table。
# app/models/component.rb
class Component < ActiveRecord::Base
...
validates :size_id, uniqueness: { scope: :ingredient_id }
...
end
# app/db/migrate/20160412134948_add_uniqueness_index_to_components_table.rb
class AddUniquenessIndexToComponentsTable
def change
add_index :components, [:size_id, :ingredient_id], unique: true, name: 'components_uniqueness_validation'
end
end
添加唯一索引的原因是为了确保与多线程服务器的唯一性。这是一篇文章,您可以在其中阅读有关 Rails、http://www.kpheasey.com/2016/02/09/thread-safe-model-uniqueness-validations/
的线程安全唯一性验证的更多信息
我有三个模型:
class Ingredient < ActiveRecord::Base
has_one :component
end
class Size < ActiveRecord::Base
has_one :component
end
class Component < ActiveRecord::Base
belongs_to :ingredient
belongs_to :size
end
我的 schema.rb
看起来像:
ActiveRecord::Schema.define(version: 20160414202240) do
create_table "components", force: :cascade do |t|
t.boolean "active"
t.decimal "cost"
t.decimal "price"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "size_id"
t.integer "ingredient_id"
end
add_index "components", ["ingredient_id"], name: "index_components_on_ingredient_id"
add_index "components", ["size_id"], name: "index_components_on_size_id"
create_table "ingredients", force: :cascade do |t|
t.string "name"
t.boolean "active"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "sizes", force: :cascade do |t|
t.string "name"
t.boolean "active"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
现在,我正在为数据库播种:
size_small = Size.create(name: 'Small', active: true)
size_medium = Size.create(name: 'Medium', active: true)
size_large = Size.create(name: 'Large', active: true)
ingredient_tomato = Ingredient.create(name: 'Tomato', active: true)
ingredient_onion = Ingredient.create(name: 'Onion', active: true)
ingredient_red_onion = Ingredient.create(name: 'Red onion', active: true)
ingredient_champignons = Ingredient.create(name: 'Champignons', active: true)
ingredient_shrimps = Ingredient.create(name: 'Shrimps', active: true)
Component.create(cost: 0.20, price: 1.00, active: true, size: size_small, ingredient: ingredient_tomato)
Component.create(cost: 0.20, price: 1.00, active: true, size: size_small, ingredient: ingredient_onion)
Component.create(cost: 0.20, price: 1.00, active: true, size: size_small, ingredient: ingredient_red_onion)
Component.create(cost: 0.30, price: 1.00, active: true, size: size_small, ingredient: ingredient_champignons)
Component.create(cost: 0.50, price: 1.50, active: true, size: size_small, ingredient: ingredient_shrimps)
Component.create(cost: 0.30, price: 1.50, active: true, size: size_medium, ingredient: ingredient_tomato)
Component.create(cost: 0.30, price: 1.50, active: true, size: size_medium, ingredient: ingredient_onion)
Component.create(cost: 0.30, price: 1.50, active: true, size: size_medium, ingredient: ingredient_red_onion)
Component.create(cost: 0.45, price: 1.50, active: true, size: size_medium, ingredient: ingredient_champignons)
Component.create(cost: 0.75, price: 2.25, active: true, size: size_medium, ingredient: ingredient_shrimps)
Component.create(cost: 0.40, price: 2.00, active: true, size: size_large, ingredient: ingredient_tomato)
Component.create(cost: 0.40, price: 2.00, active: true, size: size_large, ingredient: ingredient_onion)
Component.create(cost: 0.40, price: 2.00, active: true, size: size_large, ingredient: ingredient_red_onion)
Component.create(cost: 0.60, price: 2.00, active: true, size: size_large, ingredient: ingredient_champignons)
Component.create(cost: 1.00, price: 3.00, active: true, size: size_large, ingredient: ingredient_shrimps)
# This one uses again `size_small` and `ingredient_tomato` and shouldn't be allowed.
Component.create(cost: 2.99, price: 7.99, active: true, size: size_small, ingredient: ingredient_tomato)
什么是最rails-api
验证的方法,size
和 ingredient
组合在一起应该在 Component
table 中是唯一的?
我应该在 Component
的控制器中实现一些逻辑还是可以设置一些规则/范围/其他?
请原谅我的无知,我才刚刚开始学习Ruby(和Rails)。
我正在使用 Rails 4.2.6
和 Ruby 2.3.0p0 (2015-12-25 revision 53290)
。
提前致谢。
您将需要为模型添加范围唯一性验证并为组件添加唯一索引 table。
# app/models/component.rb
class Component < ActiveRecord::Base
...
validates :size_id, uniqueness: { scope: :ingredient_id }
...
end
# app/db/migrate/20160412134948_add_uniqueness_index_to_components_table.rb
class AddUniquenessIndexToComponentsTable
def change
add_index :components, [:size_id, :ingredient_id], unique: true, name: 'components_uniqueness_validation'
end
end
添加唯一索引的原因是为了确保与多线程服务器的唯一性。这是一篇文章,您可以在其中阅读有关 Rails、http://www.kpheasey.com/2016/02/09/thread-safe-model-uniqueness-validations/
的线程安全唯一性验证的更多信息