gpt4 book ai didi

ruby-on-rails - 在 FactoryGirl 中创建 HABTM 关系?

转载 作者:太空宇宙 更新时间:2023-11-03 16:20:40 26 4
gpt4 key购买 nike

我有以下内容:

    class Group < ActiveRecord::Base
has_and_belongs_to_many :users
has_many :permissions
has_one :city
end


class User < ActiveRecord::Base
has_many :permissions, dependent: :destroy
has_and_belongs_to_many :groups
end

FactoryGirl.define do
factory :user do
username { FFaker::Name.first_name }
email { FFaker::Internet.email }
first_name { FFaker::Name.first_name }
last_name { FFaker::Name.last_name }
current_login_ip "127.0.0.1"
login_count { rand(100) + 1 }
deleted_at nil
updated_at { FactoryGirl.generate(:time) }
created_at { FactoryGirl.generate(:time) }
suspended_at nil
password "password"
password_confirmation "password"
vertical {1}
marketplace_role {1}
end

factory :admin do
admin true
end


factory :group do
users { |a| [a.association(:user)] }
name { FFaker::Company.name }
city { FactoryGirl.create(:city) }
end
end

FactoryGirl.define do
factory :permission do
user
group
asset { fail "Please specify :asset for the permission" }
updated_at { FactoryGirl.generate(:time) }
created_at { FactoryGirl.generate(:time) }
end
end

factory :city do
name { FFaker::Name.name }
end

factory :zone do
city
name { FFaker::Name.name }
end

factory :location do
zone
name { FFaker::Name.name }
end

Failure/Error: @group = FactoryGirl.create(:group) ActiveModel::MissingAttributeError: can't write unknown attribute group_id

这就是我得到的。我无法定义和使用组工厂。我调查了这个post , 但解决方案不起作用。

最佳答案

将city factory改成下面的

 factory :city do
group nil # or group_id if you will
name { FFaker::Name.name }
end

你很可能会表现出色。

关于ruby-on-rails - 在 FactoryGirl 中创建 HABTM 关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34018768/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com