通过关联创建时不会运行验证

时间:2014-10-18 11:46:03

标签: ruby-on-rails ruby ruby-on-rails-4

我有以下两个模型

  has_many :signups, foreign_key: 'tournament_id', class_name: 'TournamentSignup', dependent: :destroy

def sign_up(team)
   #signups.create team: team #This does not work
   TournamentSignup.create tournament: self, team: team #This works

TournamentSignup

  belongs_to :tournament
  belongs_to :team, class_name: 'TournamentTeam', foreign_key: 'tournament_team_id'

  validates :team, presence: true
  validates :tournament, presence: true

失败的测试用例

it 'should not allow nil to sign up' do
team = nil
tournament = create(:tournament, type: 'Dota2Tournament')
tournament.sign_up team

signups = tournament.signups

expect(signups.size).to eq(0)
end

使用第一种方法时,不会运行验证。为什么会这样? 使用rails 4.0.2

0 个答案:

没有答案