加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – FactoryGirl belongs_to association

发布时间:2020-12-17 03:13:41 所属栏目:百科 来源:网络整理
导读:我有一个工厂,我在工厂/ 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" endend 然后我想定义一个属于一个位置的健身营(因
我有一个工厂,我在工厂/ 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

我错过了什么吗?这是否表示我的模型可能配置不正确?

谢谢!

蒂姆

解决方法

我是个白痴 – 我有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

雷达(Ryan Bigg)的疯狂道具帮助我度过了这个.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读