问题描述
我在通过FactoryGirl验证数据时遇到问题.
I have problems on validating my data through FactoryGirl.
对于team.rb,自定义验证为has_only_one_leader和belongs_to_same_department
For team.rb, the custom validations are has_only_one_leader and belongs_to_same_department
class Team < ActiveRecord::Base
attr_accessible :name, :department, :active
has_many :memberships
has_many :users, through: :memberships
accepts_nested_attributes_for :memberships, :users
validates :department, inclusion: [nil, "Architectural", "Interior Design"]
validates_uniqueness_of :name, scope: :department, conditions: -> { where(active: true) }
validate :has_only_one_leader
validate :belongs_to_same_department
def has_only_one_leader
unless self.users.where!(designation: "Team Leader").size == 1
errors.add(:team, "needs to have exactly one leader")
end
end
def belongs_to_same_department
unless self.users.where!.not(department: self.department).size == 0
errors.add(:users, "should belong to the same department")
end
end
我还将包括Membership.rb和user.rb(仅关联),仅供参考.
I'll also include membership.rb and user.rb (associations only) just for reference.
class Membership < ActiveRecord::Base
belongs_to :team
belongs_to :user
end
class User < ActiveRecord::Base
has_many :memberships
has_many :teams, through: :memberships
end
这是我的team.rb工厂
Here's my factory for team.rb
FactoryGirl.define do
factory :team do
sequence(:name) {|n| "Team #{n}" }
department "Architectural"
before(:create) do |team|
team.users << FactoryGirl.create(:user, designation: "Team Leader",
department: "Architectural")
team.users << FactoryGirl.create_list(:user, 5,
designation: "CAD Operator", department: "Architectural")
end
end
end
似乎在Rails控制台中执行FactoryGirl.create(:team)
后,似乎从验证中得到了错误消息.
It seems that after I do FactoryGirl.create(:team)
in the rails console, it seems that I got the error messages from my validations.
当我手动组建团队时,我注意到了两件事,特别是在团队中添加了1名领导者和5个属于同一部门的成员.
Two things I've noticed when I manually build a team, specifically adding members to the team with 1 leader and 5 members belonging to the same department:
-
team.users.where!(designation: "Team Leader").size
返回6
,尽管只有一个领导者,将名称更改为CAD Operator也是如此,返回6
,尽管团队中只有五个非领导者.
team.users.where!(designation: "Team Leader").size
returns6
although there's only one leader, same goes for changing the designation to CAD Operator, returns6
although there are only five non leaders in the team.
使用相同的命令检查成员是否在同一部门,尽管所有成员都属于同一部门,team.users.where!.not(department: team.department).size
返回6
.
same goes for checking if the members are in the same department, team.users.where!.not(department: team.department).size
returns 6
, although all of them belong to the same department.
我的模型或工厂中是否要进行任何修改?
Are there any modifications to make in my model or in my factory?
推荐答案
以下是我在获得答案之前发现的东西.
Here are the things that I've discovered before getting the answer.
-
team.users.where(designation: "Team Leader")
返回一个User::ActiveRelation_AssociationRelation
对象 -
team.users
返回User::ActiveRecord_Associations_CollectionProxy
对象 -
CollectionProxy
没有方法where
,因为此方法(在我看来)是对数据库的查询(如果团队已经保存在数据库中,则可以使用where
,但例外)情况下,仅用于建筑)
team.users.where(designation: "Team Leader")
returns aUser::ActiveRelation_AssociationRelation
objectteam.users
returns aUser::ActiveRecord_Associations_CollectionProxy
objectCollectionProxy
has no methodwhere
since the this method (in my opinion) is a query to the database (with exception if the team is already saved in the database, you can usewhere
, but in this case, it's only for building)
因此,我将select
方法与count
一起使用,以返回正确的值,如下所示.我将使用问题中的示例来说明答案.
Therefore, I used the select
method accompanied with the count
, to return the correct values like so. I'll use my example from the question to illustrate the answer.
-
team.users.select(:designation) {|user| user.designation == "Team Leader"}.count
返回1
-
team.users.select(:department) {|user| user.department != team.department}.count
返回0
team.users.select(:designation) {|user| user.designation == "Team Leader"}.count
returns1
team.users.select(:department) {|user| user.department != team.department}.count
returns0
这篇关于通过FactoryGirl验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!