Rails 3 验证 - 关联属性、RSpec 和 Factory Girl

发布于 2024-11-07 06:54:53 字数 2109 浏览 4 评论 0原文

我正在尝试在 Rails 3 中编写一个验证类,它将检查是否存在至少一个具有特定属性值的关联对象。

在我的场景中,一个帐户可以有许多用户。用户模型有一个“角色”字符串字段。一个帐户必须至少有一个角色等于“admin”的用户。

以下代码在我的测试期间被破坏(我正在使用 rspec 和 Factory Girl)。执行测试时,account_id 返回为 null。

这是模型:

class Account < ActiveRecord::Base
  validates :name, :presence => true, :uniqueness => true
  validate :has_one_admin
  has_many :users
  attr_accessible :name

  private 

  def has_one_admin
    User.where(:role => "admin", :account_id => self.id).count > 0
  end
end

这是 rspec 抛出的错误:

Error message being thrown:   Failure/Error: @account = Factory(:account_with_admin)
 ActiveRecord::StatementInvalid:
   SQLite3::SQLException: no such column: users.account_id: SELECT COUNT(*) FROM "users" WHERE "users"."role" = 'admin' AND "users"."account_id" IS NULL

这是 RSpec 测试:

require 'spec_helper'

describe Team do

   # - Removed -  
   # before(:each) do
   #   @account = Factory(:account)
   #  @attr = { :name => "Testing Team"}
   # end

  # Modified after discussion...
  before(:each) do
    @user = Factory(:user)
    @act_attr = { :name => "My Account",
                  :users => @user }
    @account = Account.new(@act_attr)
    @attr = { :name => "Testing Team"}
  end
  # End of modification


  it "must have a name" do
    no_name_team = Team.new(@attr.merge(:name  => ""))
    no_name_team.should_not be_valid
  end ...

最后,我的工厂文件:

Factory.define :admin, :class => User do |f|
  f.email '[email protected]'
  f.password  'password'
  f.role 'admin'
end

Factory.define :team, :class => Team do |f|
f.name 'Testing Team'   
end

Factory.define :account, :class => Account do |f|
    f.name 'Testing Account'
end

Factory.define :account_with_admin, :parent => :account do |f|
  f.after_create { |a| Factory(:admin, :account => a) }
end

我的假设是我在对象的持久化之前跳到某处,但我只是不确定在哪里。感谢您的帮助/指导!

I'm trying to write a validation class in rails 3 that will check for the presence of at least one associated object that has a particular property value.

In my scenario, an Account can have many Users. The User model has a "Role" string field. An account must have at least one User with a Role that equals "admin".

The following code is breaking during my testing (I'm using rspec & Factory Girl). When the tests are executed, the account_id is returning as null.

Here is the model:

class Account < ActiveRecord::Base
  validates :name, :presence => true, :uniqueness => true
  validate :has_one_admin
  has_many :users
  attr_accessible :name

  private 

  def has_one_admin
    User.where(:role => "admin", :account_id => self.id).count > 0
  end
end

Here is the error being thrown by rspec:

Error message being thrown:   Failure/Error: @account = Factory(:account_with_admin)
 ActiveRecord::StatementInvalid:
   SQLite3::SQLException: no such column: users.account_id: SELECT COUNT(*) FROM "users" WHERE "users"."role" = 'admin' AND "users"."account_id" IS NULL

Here is the RSpec test:

require 'spec_helper'

describe Team do

   # - Removed -  
   # before(:each) do
   #   @account = Factory(:account)
   #  @attr = { :name => "Testing Team"}
   # end

  # Modified after discussion...
  before(:each) do
    @user = Factory(:user)
    @act_attr = { :name => "My Account",
                  :users => @user }
    @account = Account.new(@act_attr)
    @attr = { :name => "Testing Team"}
  end
  # End of modification


  it "must have a name" do
    no_name_team = Team.new(@attr.merge(:name  => ""))
    no_name_team.should_not be_valid
  end ...

And finally, my factory file:

Factory.define :admin, :class => User do |f|
  f.email '[email protected]'
  f.password  'password'
  f.role 'admin'
end

Factory.define :team, :class => Team do |f|
f.name 'Testing Team'   
end

Factory.define :account, :class => Account do |f|
    f.name 'Testing Account'
end

Factory.define :account_with_admin, :parent => :account do |f|
  f.after_create { |a| Factory(:admin, :account => a) }
end

My assumption is that I'm jumping ahead of the persisting of the object somewhere but I'm just not sure where. Thanks for any help / direction!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文