Mongoid保存文档,尽管自定义验证无效

时间:2022-10-05 10:22:37

I'm not sure if this is a problem with Mongoid or with the standard Rails validators, but documents that are invalid are still being saved to the database.

我不确定这是Mongoid或标准Rails验证器的问题,但是无效的文档仍然保存到数据库中。

I have models set up to something like:

我的模型设置为:

class League
  include Mongoid::Document

  has_many :teams

  validate do
    if teams.size > 12
      errors.add(:teams, 'too many teams')
    end
  end
end

class Team
  include Mongoid::Document

  belongs_to :league
end

I would expect the following test to pass, but it does not. Instead of my custom validation preventing more than 12 Teams from being added to a League, the league gets saved with 13 teams anyway.

我希望以下测试能够通过,但事实并非如此。而不是我的自定义验证防止超过12个团队被添加到联盟,无论如何联盟得到了13个团队的保存。

# Factory for a League.
FactoryGirl.define do
  factory :league do
  name "Test League"

  factory :league_with_teams do
    ignore do
      teams_count 5
    end

    after(:create) do |league, evaluator|
      FactoryGirl.create_list(:team, 
                              evaluator.teams_count, 
                              league: league)
    end
  end
end

describe League do
  it "should not allow more than 12 teams" do
    league = FactoryGirl.create(:league_with_teams, teams_count: 12)
    league.teams << FactoryGirl.create(:team)
    league.should_not be_valid # passes
    League.find(league.id).teams.size.should eq(12) # fails
  end
end

The funny thing is, if I change the line in the test which adds the 13th team to use build instead of create, league.teams << FactoryGirl.build(:team), then the test passes. However this is not a solution because I want to guarantee that a League cannot have more than 12 teams regardless of whether or not the teams being added are new or already in the DB.

有趣的是,如果我在测试中更改了第13个团队使用build而不是create,league.teams << FactoryGirl.build(:team),则测试通过。然而,这不是一个解决方案,因为我想保证联盟不能拥有超过12支球队,无论所添加的球队是新的还是已经在数据库中。

Is there anyway to guarantee this?

无论如何要保证这个吗?

Edit

编辑

Adding a validator to the Team model, like I did below, doesn't seem to work either.

像我在下面所做的那样,向Team模型添加验证器似乎也不起作用。

class Team
  include Mongoid::Document

  belongs_to :league

  validate do |team|
    if league.teams.size > 12
      errors.add :base, "cannot have more than 12 teams in a league"
    end
  end
end

I believe the problem has to do with the fact that << and push are atomic operations, and therefore they skip callbacks and validations. That being said, this must be a fairly common usecase, no? So how are other people validating the number of documents being referenced?

我认为问题与< <和push是原子操作这一事实有关,因此它们会跳过回调和验证。话虽这么说,这必须是一个相当普遍的用例,不是吗?那么其他人如何验证被引用的文档数量呢?< p>

1 个解决方案

#1


0  

I dont think the problem is with << or push methods because them both should trigger model's validations.

我不认为问题是使用< <或push方法,因为它们都应该触发模型的验证。< p>

Try using:

尝试使用:

league.teams << FactoryGirl.create(:team, league: league)

league.teams << FactoryGirl.create(:team,league:league)

#1


0  

I dont think the problem is with << or push methods because them both should trigger model's validations.

我不认为问题是使用< <或push方法,因为它们都应该触发模型的验证。< p>

Try using:

尝试使用:

league.teams << FactoryGirl.create(:team, league: league)

league.teams << FactoryGirl.create(:team,league:league)