Ruby on rails 如何将此特定构造函数放入FactoryGirl对象中?
我正在尝试将创建代码放入FactoryGirl中 所以基本上在这里,你可以看到我的类用户有一个方法,从他的信息,id,created_at,等等创建一张图片 然后我试着把它放到FactoryGirl对象中,如下所示Ruby on rails 如何将此特定构造函数放入FactoryGirl对象中?,ruby-on-rails,ruby,factory-bot,Ruby On Rails,Ruby,Factory Bot,我正在尝试将创建代码放入FactoryGirl中 所以基本上在这里,你可以看到我的类用户有一个方法,从他的信息,id,created_at,等等创建一张图片 然后我试着把它放到FactoryGirl对象中,如下所示 FactoryGirl.define do factory :user do sequence(:name) { |i| "flav_#{i}" } end factory :picture do store_dir user.get_user_med
FactoryGirl.define do
factory :user do
sequence(:name) { |i| "flav_#{i}" }
end
factory :picture do
store_dir user.get_user_medias_path
remote_url "http://lorempixel.com/600/600/animals/internet#{user.id}/"
association :user
end
end
但是正如您所期望的,他既找不到user.id也找不到user.method
我应该如何在图片对象中引用用户属性
我迷路了。我们非常欢迎任何帮助。=) 在您的代码中,
用户
在工厂定义的上下文中被调用,而工厂定义没有这样的方法。您需要在实际构建的对象上调用user
,如图所示
为此,应将计算值包装在块中:
factory :picture do
store_dir { user.get_user_medias_path }
remote_url { "http://lorempixel.com/600/600/animals/internet#{user.id}/" }
association :user
end
请参见文档中的和属性
factory :picture do
store_dir { user.get_user_medias_path }
remote_url { "http://lorempixel.com/600/600/animals/internet#{user.id}/" }
association :user
end