gpt4 book ai didi

ruby-on-rails - 如何确保该表不包含重复项(基于两个外键)?

转载 作者:数据小太阳 更新时间:2023-10-29 09:00:36 25 4
gpt4 key购买 nike

我有三个模型:

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 验证的方法, sizeingredient 组合在一起应该在 Component 中是唯一的 表?

我应该在 Component 的 Controller 中实现一些逻辑,还是可以设置一些规则/范围/其他什么?

请原谅我的无知,我才刚刚开始学习 Ruby(和 Rails)。

我正在使用 Rails 4.2.6Ruby 2.3.0p0(2015-12-25 修订版 53290)

提前致谢。

最佳答案

您将需要向模型添加范围唯一性验证,并向组件表添加唯一索引。

# 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/

关于ruby-on-rails - 如何确保该表不包含重复项(基于两个外键)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36633905/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com