Ruby on rails 有一个关联在rails中创建多个记录吗

Ruby on rails 有一个关联在rails中创建多个记录吗,ruby-on-rails,ruby,activerecord,rails-activerecord,Ruby On Rails,Ruby,Activerecord,Rails Activerecord,我有一个用户模型和商店模型。我想允许用户只创建一个商店。所以在我的用户模型中 class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable,

我有一个用户模型和商店模型。我想允许用户只创建一个商店。所以在我的用户模型中

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  validates :terms_and_conditions, :acceptance => true
  has_one :shop
end
class用户true
有一个:商店
终止
我的商店模型是这样的

class Shop < ApplicationRecord
  has_many :products, dependent: :destroy
  belongs_to :user
end
class-Shop
但在控制台中,若我尝试为已经有一个商店的用户创建一个新商店,那个么并没有错误并且提交成功

[
  #<Shop id: 1, name: "Rabin & Rose Shop", location: "Banepa Kavre Nepal", description: "oho k vhk kl;o jjio ko;k; jljlkj", rating: nil, delivery_service: true, user_id: 1, created_at: "2017-09-27 15:31:57", updated_at: "2017-09-27 15:31:57", img_url: nil>, 
  #<Shop id: 2, name: "jhoney", location: "sins shop", description: "the entire fuck history here", rating: nil, delivery_service: true, user_id: 1, created_at: "2017-09-28 00:55:44", updated_at: "2017-09-28 00:55:44", img_url: nil>, 
  #<Shop id: 3, name: "Thakur Shop", location: "Pulbazar banepa", description: "Our shop has chicken bedroom. you can met call gir...", rating: nil, delivery_service: true, user_id: 2, created_at: "2017-09-28 01:50:40", updated_at: "2017-09-28 01:50:40", img_url: nil>, 
  #<Shop id: 4, name: nil, location: nil, description: nil, rating: nil, delivery_service: true, user_id: 1, created_at: "2017-09-28 03:49:34", updated_at: "2017-09-28 03:49:34", img_url: nil>
]
[
#, 
#, 
#, 
#
]
用户id=1
有多条记录。

Rails关联方法(
有一条
有多条
,等等)不会验证您的数据。也就是说,数据仍然可以插入,因为您没有在应用程序层或数据层对传入数据进行验证

为了限制用户拥有多个店铺,必须在数据层添加唯一索引,必要时在应用层添加唯一性验证。一旦建立起来,数据库将不允许为一个特定用户创建多个商店

迁移上的唯一索引应该如下所示

add_index :shops, :user_id, unique: true
使用该索引,数据库将不允许插入具有重复用户id的店铺记录

在应用层,您可以在用户上添加唯一性验证,或在
商店
上添加用户id,或在
用户
上添加验证,以检查是否存在
商店
,并在验证失败时让其添加错误

这就是一个例子

class Shop < ApplicationRecord
  validate :one_shop_per_user

  private

  def one_shop_per_user
     if user.shop && user.shop != self
       errors.add(:user, "already has a shop")
     end
  end
end
class-Shop
请注意,这只是实现您想要做的事情的众多解决方案之一

Rails关联方法(
有一个
有许多
,等等)不会验证您的数据。也就是说,数据仍然可以插入,因为您没有在应用程序层或数据层对传入数据进行验证

为了限制用户拥有多个店铺,必须在数据层添加唯一索引,必要时在应用层添加唯一性验证。一旦建立起来,数据库将不允许为一个特定用户创建多个商店

迁移上的唯一索引应该如下所示

add_index :shops, :user_id, unique: true
使用该索引,数据库将不允许插入具有重复用户id的店铺记录

在应用层,您可以在用户上添加唯一性验证,或在
商店
上添加用户id,或在
用户
上添加验证,以检查是否存在
商店
,并在验证失败时让其添加错误

这就是一个例子

class Shop < ApplicationRecord
  validate :one_shop_per_user

  private

  def one_shop_per_user
     if user.shop && user.shop != self
       errors.add(:user, "already has a shop")
     end
  end
end
class-Shop
请注意,这只是实现您想要做的事情的众多解决方案之一