工厂女孩多次has_many通过
我需要创建一些由多个工厂组成的工厂
这是我的模特
Topic has_many :plan_topics has_many :plans, :through => :plan_topics PlanTopic belongs_to :plan belongs_to :topic Plan has_many :subscriptions has_many :members, :through => :subscriptions has_many :plan_topics has_many :topics, :through => :plan_topics Subscription belongs_to :member belongs_to :plan Member has_many :subscriptions has_many :plans, :through => :subscriptions
这就是我所拥有的
Factory.define :topic do |topic| topic.name "Operations" end Factory.define :plan do |plan| plan.title "A test Finance plan" plan.price "200" end Factory.define :plan_topic do |plan_topic| plan_topic.topic {|topic| topic.association(:topic)} plan_topic.plan {|plan| plan.association(:plan)} end
我想做的是 – 工厂(:member_with_subscription)
Factory.define :member_with_subscription do |subscription| subscription.association(:plan_with_topic) subscription.association(:member) end
有办法做到这一点吗?
考虑使用after_build
回调来设置所有必需的依赖项。 例如:
Factory.define :member_with_subscription, :class => 'Member' do |m| m.after_build do |member| member.subscriptions << Factory.build(:subscription) end end
我的做法略有不同,这种方式可能稍微容易理解:
FactoryGirl.define do factory :member_with_description do after(:build) do |member| member.subscriptions << FactoryGirl.build(:subscription) end end end
- Rails:NameError:未初始化的常量OrderItem
- 使用虚假数据生成Paperclip图像上传 – Ruby on Rails Populator / Faker Gems
- 在Rails中使用titleize作为首字母缩略词
- 保存PDFKit中间件显示的PDF文件
- 使用用户名设计无需密码的身份validation
- Rails服务器正在运行,但无法连接/ ping / telnet到localhost
- 在Rails中添加另一个列之前的列
- 升级到Rails 4后,postgresql服务器上的内存泄漏
- 是否可以使用Ruby 1.9.3在Heroku上运行我的Rails应用程序? 如果是这样,怎么样?