将数据库模型转换为模型 Ruby Rails
Translating Database Models to Models on Ruby On Rails
我开始 Ruby Rails 通过学校的 purchase/resale 平台项目。当我尝试从我的关系模型转换它们时,我的模型出现问题。
首先,我对我的数据库进行了建模。这是简化的实体关系模型:
然后我将其翻译成关系模型:
最后,我在 Ruby Rails 上实现了它。
我实现了一个模型客户端:
class Client < ApplicationRecord
attr_accessor :name
validates :name, :presence => true
has_many :purchasings, :dependent => :destroy
has_many :sellers, :through => :purchasings
has_many :articles, :through => :purchasings
end
我已经实现了一个模型卖家:
class Seller < ApplicationRecord
attr_accessor :name
validates :name, :presence => true
has_many :purchasings, :dependent => :destroy
has_many :sellers, :through => :purchasings
has_many :articles, :through => :purchasings
end
我实现了一个模型文章
class Article < ApplicationRecord
attr_accessor :quantity
validates :quantity, :presence => true
has_one :purchasing, :dependent => :destroy
has_one :client, :through => :purchasings
has_one :seller, :through => :purchasings
end
我已经实施了一个采购模型:
class Purchasing < ApplicationRecord
attr_accessor :client_id, :seller_id, :article_id
belongs_to :client, :class_name => "Client"
belongs_to :seller, :class_name => "Seller"
belongs_to :article, :class_name => "Article"
validates :client_id, :presence => true
validates :seller_id, :presence => true
validates :article_id, :presence => true
end
我修改了采购数据库迁移:
class CreatePurchasing < ActiveRecord::Migration[5.1]
def change
[...]
add_index :purchasings, :client_id
add_index :purchasings, :seller_id
add_index :purchasings, :article_id
add_index :purchasings, [:client_id, :seller_id], :unique => true
end
def down
[...]
end
end
我知道这是不正确的,因为当我在 Rails 控制台上执行以下代码时:
cl1 = Client.create(:name => "John")
cl2 = Client.create(:name => "James")
sel1 = Seller.create(:nom => "Jack")
sel2 = Seller.create(:nom => "Jil")
a1 = Article.create(:quantity => 5)
p1 = Purchasing.new(:client => cl1, :client_id => cl1.id, :seller => sel1, :seller_id => sel1.id, :article => a1, :article_id => a1.id)
p1.save
p2 = Purchasing.new(:client => cl2, :client_id => cl2.id, :seller => sel1, :seller_id => sel1.id, :article => a1, :article_id => a1.id)
p2.save
p2.save
returns 是的,而一件物品不能由同一卖家出售而由两个不同的客户购买。
您正在为采购的错误列添加索引 table。根据要求,article_id 和 seller_id 最好不要重复。所以你实际上需要的是 seller_id 和 article_id 列的唯一性约束。您可以通过在数据库层上的两列 seller_id 和文章 ID 的组合上创建唯一索引来实现。您还应该在购买模型上添加应用层验证。
class Purchasing < ApplicationRecord
attr_accessor :client_id, :seller_id, :article_id
belongs_to :client, :class_name => "Client"
belongs_to :seller, :class_name => "Seller"
belongs_to :article, :class_name => "Article"
validates :client_id, :presence => true
validates :seller_id, :presence => true
validates :article_id, :presence => true
validates :article_id, uniqueness: {scope: :seller_id}
end
现在你也应该写一个数据库迁移来为这两列添加唯一索引。
class AddUniquenessConstraintInPurshasing < ActiveRecord::Migration
def change
add_index :purchasings, [:article_id, :seller_id], :unique => true
end
结束
我开始 Ruby Rails 通过学校的 purchase/resale 平台项目。当我尝试从我的关系模型转换它们时,我的模型出现问题。
首先,我对我的数据库进行了建模。这是简化的实体关系模型:
然后我将其翻译成关系模型:
最后,我在 Ruby Rails 上实现了它。
我实现了一个模型客户端:
class Client < ApplicationRecord attr_accessor :name validates :name, :presence => true has_many :purchasings, :dependent => :destroy has_many :sellers, :through => :purchasings has_many :articles, :through => :purchasings end
我已经实现了一个模型卖家:
class Seller < ApplicationRecord attr_accessor :name validates :name, :presence => true has_many :purchasings, :dependent => :destroy has_many :sellers, :through => :purchasings has_many :articles, :through => :purchasings end
我实现了一个模型文章
class Article < ApplicationRecord attr_accessor :quantity validates :quantity, :presence => true has_one :purchasing, :dependent => :destroy has_one :client, :through => :purchasings has_one :seller, :through => :purchasings end
我已经实施了一个采购模型:
class Purchasing < ApplicationRecord attr_accessor :client_id, :seller_id, :article_id belongs_to :client, :class_name => "Client" belongs_to :seller, :class_name => "Seller" belongs_to :article, :class_name => "Article" validates :client_id, :presence => true validates :seller_id, :presence => true validates :article_id, :presence => true end
我修改了采购数据库迁移:
class CreatePurchasing < ActiveRecord::Migration[5.1] def change [...] add_index :purchasings, :client_id add_index :purchasings, :seller_id add_index :purchasings, :article_id add_index :purchasings, [:client_id, :seller_id], :unique => true end def down [...] end end
我知道这是不正确的,因为当我在 Rails 控制台上执行以下代码时:
cl1 = Client.create(:name => "John")
cl2 = Client.create(:name => "James")
sel1 = Seller.create(:nom => "Jack")
sel2 = Seller.create(:nom => "Jil")
a1 = Article.create(:quantity => 5)
p1 = Purchasing.new(:client => cl1, :client_id => cl1.id, :seller => sel1, :seller_id => sel1.id, :article => a1, :article_id => a1.id)
p1.save
p2 = Purchasing.new(:client => cl2, :client_id => cl2.id, :seller => sel1, :seller_id => sel1.id, :article => a1, :article_id => a1.id)
p2.save
p2.save
returns 是的,而一件物品不能由同一卖家出售而由两个不同的客户购买。
您正在为采购的错误列添加索引 table。根据要求,article_id 和 seller_id 最好不要重复。所以你实际上需要的是 seller_id 和 article_id 列的唯一性约束。您可以通过在数据库层上的两列 seller_id 和文章 ID 的组合上创建唯一索引来实现。您还应该在购买模型上添加应用层验证。
class Purchasing < ApplicationRecord
attr_accessor :client_id, :seller_id, :article_id
belongs_to :client, :class_name => "Client"
belongs_to :seller, :class_name => "Seller"
belongs_to :article, :class_name => "Article"
validates :client_id, :presence => true
validates :seller_id, :presence => true
validates :article_id, :presence => true
validates :article_id, uniqueness: {scope: :seller_id}
end
现在你也应该写一个数据库迁移来为这两列添加唯一索引。
class AddUniquenessConstraintInPurshasing < ActiveRecord::Migration
def change
add_index :purchasings, [:article_id, :seller_id], :unique => true
end
结束