Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 具有关联的FactoryGirl模型规范_Ruby On Rails_Rspec_Associations_Factory Bot - Fatal编程技术网

Ruby on rails 具有关联的FactoryGirl模型规范

Ruby on rails 具有关联的FactoryGirl模型规范,ruby-on-rails,rspec,associations,factory-bot,Ruby On Rails,Rspec,Associations,Factory Bot,测试新手,我正在尝试将FactoryGirl添加到模型测试中。我的挑战是,我在用户和帐户之间有一个has_-one关系,但是通过帐户模型中的owner_-id字段 下面的测试工作 describe Client do it "is valid with a name and account" do user = User.create(email: "me@example.com", password: "pw", password_confirmation: "pw") a

测试新手,我正在尝试将FactoryGirl添加到模型测试中。我的挑战是,我在用户和帐户之间有一个has_-one关系,但是通过帐户模型中的owner_-id字段

下面的测试工作

describe Client do
  it "is valid with a name and account" do
    user = User.create(email: "me@example.com", password: "pw", password_confirmation: "pw")
    account = Account.create(name: "Company One", owner_id: user.id)
    client = account.clients.new(name: "TestClient")
    expect(client).to be_valid
  end
end
我正在尝试修改,以便使用FactoryGirl:

describe Client do
  it "has a valid factory" do
    expect(build(:client)).to be_valid
  end
end
客户机模型属于拥有一个用户的帐户。关联不是通过用户id,而是通过所有者id

class User < ActiveRecord::Base
  has_one :owned_account, class_name: 'Account', foreign_key: 'owner_id'
  has_many :user_accounts, dependent: :destroy
  has_many :accounts, through: :user_accounts
end

class Account < ActiveRecord::Base
  belongs_to :owner, class_name: 'User'
  has_many :user_accounts, dependent: :destroy
  has_many :users, through: :user_accounts
  has_many :clients, dependent: :destroy
end

class Client < ActiveRecord::Base
  belongs_to :account
  default_scope { where(account_id: Account.current_id)}
end
如何从关联中获取用户id并分配给帐户的所有者id

FactoryGirl.define do
  factory :account do
    association :owner, factory: :user
    name "Account One"
  end
end
我需要将帐户工厂中的关联的:owner\u id更改为:owner

FactoryGirl.define do
  factory :account do
    association :owner, factory: :user
    name "Account One"
  end
end