Ruby on rails Rails、validate通过与子级的属性值关联而具有许多功能

Ruby on rails Rails、validate通过与子级的属性值关联而具有许多功能,ruby-on-rails,ruby,Ruby On Rails,Ruby,我有三种型号: class User < ApplicationRecord has_many :game_accounts has_many :favorite_game_accounts, through: :game_account_favorites, source: :game_account end class GameAccount < ApplicationRecord belongs_to :user has_many :favor

我有三种型号:

class User < ApplicationRecord
    has_many :game_accounts
    has_many :favorite_game_accounts, through: :game_account_favorites, source: :game_account
end

class GameAccount < ApplicationRecord
    belongs_to :user
    has_many :favorite_users, through: :game_account_favorites, source: :user
end

class GameAccountFavorite < ApplicationRecord
    belongs_to :user
    belongs_to :game_account

    validates_presence_of :user, :game_account
    validates_uniqueness_of :user, scope: :game_account_id
end
class用户
这意味着
用户
可以拥有自己的
游戏帐户
,其他
用户
可以将其添加到收藏夹中


我添加了
范围
,以防止一个用户拥有同一
游戏帐户的多个收藏夹
。然而,有一个问题。用户可以将自己的游戏帐户添加到收藏夹。如何防止用户将自己的
GameAccount
添加到收藏夹?

我不确定是否有任何内置的Rails验证用于您的案例,因此我建议您编写自己的自定义帐户

在您的特定情况下,您可以在
GameAccountFavorite
实例上验证
game\u account.id
不等于
user.id

有很多方法可以解决这个问题