Ruby on rails 验证是否存在多个/新记录的父项?情况
那么,我的问题是:我是否应该继续删除该验证器,并确保在整个代码库中始终设置Ruby on rails 验证是否存在多个/新记录的父项?情况,ruby-on-rails,validation,activerecord,Ruby On Rails,Validation,Activerecord,那么,我的问题是:我是否应该继续删除该验证器,并确保在整个代码库中始终设置move.game?或者有没有一种神奇的方法来保存它,并且仍然使用Game.new.moves通过更多的研究,我发现有一种神奇的方法。我只需要在关系的两侧添加inverse\u的选项。不确定这是如何帮助ActiveRecord知道如何通过验证程序的,但它是有效的!以下是更新的型号: move = current_user.moves.build move.game = @game move.save 类游戏
move.game
?或者有没有一种神奇的方法来保存它,并且仍然使用Game.new.moves通过更多的研究,我发现有一种神奇的方法。我只需要在关系的两侧添加inverse\u的
选项。不确定这是如何帮助ActiveRecord知道如何通过验证程序的,但它是有效的!以下是更新的型号:
move = current_user.moves.build
move.game = @game
move.save
类游戏
这个答案与解决方案相反:经过进一步研究,我发现有一种神奇的方法。我只需要在关系的两侧添加inverse\u的
选项。不确定这是如何帮助ActiveRecord知道如何通过验证程序的,但它是有效的!以下是更新的型号:
move = current_user.moves.build
move.game = @game
move.save
类游戏
这个答案与解决方案相反:
class Game < ActiveRecord::Base
has_many :moves, inverse_of: :game
end
class Move < ActiveRecord::Base
belongs_to :game, inverse_of: :moves
belongs_to :user
validates_presence_of :user
validates_presence_of :game
end
#####
game = Game.new
move = current_user.moves.build
game.moves << move
game.save # => true