FactoryGirl 属于协会
我有一个工厂,我在factories/locations.rb 中定义了一个位置。我正在使用 Mongoid 和 Rails 3.1.1 以及 ruby 1.9.3。
FactoryGirl.define do factory :location do name Faker::Name.name description "Down by the river" end end
然后我想定义一个属于某个位置的健身营(因此具有 location_id 属性)。
FactoryGirl.define do factory :fitness_camp do title "Parkour" association :location_id, :factory => :location end end
这有效,但是,这是我黑客攻击的结果,而不是我在文档中读到的结果。从入门指南 (https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED. md)看来这应该很简单:
factory :fitness_camp do title "Parkour" location end
我错过了什么吗?这是否表明我的模型可能配置不正确?
谢谢!
蒂姆
I have a factory where I define a location in factories/locations.rb
. I'm using Mongoid and Rails 3.1.1 with ruby 1.9.3.
FactoryGirl.define do factory :location do name Faker::Name.name description "Down by the river" end end
And then I want to define a fitness camp which belongs_to a location (and therefore has a location_id attribute).
FactoryGirl.define do factory :fitness_camp do title "Parkour" association :location_id, :factory => :location end end
This works but, is the result of my hacking, not what I read in the docs. From the getting started guide ( https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md) it seems this should be as simple as:
factory :fitness_camp do title "Parkour" location end
Am I missing something? Does this indicate my models might not be configured correctly?
Thanks!
Tim
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
validates_numericality_of :location_id
疯狂支持,因为他帮助我解决了这个问题。我是个白痴——我对 Radar (Ryan Bigg) 的
I was an idiot -- I had
validates_numericality_of :location_id
Mad props to Radar (Ryan Bigg) for helping me through this.