FactoryGirl 属于协会

发布于 2024-12-15 17:11:02 字数 789 浏览 1 评论 0原文

我有一个工厂,我在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不即不离 2024-12-22 17:11:02

validates_numericality_of :location_id疯狂支持,因为他帮助我解决了这个问题。

class FitnessCamp

  include Mongoid::Document

  field :title, :type => String

  belongs_to :location

  validates_presence_of  :location_id, :title
  validates_numericality_of :location_id

我是个白痴——我对 Radar (Ryan Bigg) 的

I was an idiot -- I had validates_numericality_of :location_id

class FitnessCamp

  include Mongoid::Document

  field :title, :type => String

  belongs_to :location

  validates_presence_of  :location_id, :title
  validates_numericality_of :location_id

Mad props to Radar (Ryan Bigg) for helping me through this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文