我有两个模特,Show和Venue。场地has_many :shows
。节目belongs to :venue
。它们的定义如下:
class Venue < ActiveRecord::Base
validates :name, presence: true
has_many :shows
end
class Show < ActiveRecord::Base
validates :show_date, presence: true
validates :doors_open, presence: true
validates :dinner_starts, presence: true
validates :dinner_ends, presence: true
validates :show_starts, presence: true
validates :show_ends, presence: true
validates :venue, presence: true
belongs_to :venue, foreign_key: 'venue_id'
end
with schema:
ActiveRecord::Schema.define(version: 20150218021116) do
create_table 'shows', force: :cascade do |t|
t.date 'show_date'
t.time 'doors_open'
t.time 'dinner_starts'
t.time 'dinner_ends'
t.time 'show_starts'
t.time 'show_ends'
t.datetime 'created_at', null: false
t.datetime 'updated_at', null: false
t.integer 'venue_id'
end
add_index 'shows', ['venue_id'], name: 'index_shows_on_venue_id'
create_table 'venues', force: :cascade do |t|
t.string 'name'
t.datetime 'created_at', null: false
t.datetime 'updated_at', null: false
end
end
和工厂:
FactoryGirl.define do
factory :show do
show_date Faker::Date.forward 30
doors_open Faker::Time.forward 30, :evening
dinner_starts Faker::Time.forward 30, :evening
dinner_ends Faker::Time.forward 30, :evening
show_starts Faker::Time.forward 30, :evening
show_ends Faker::Time.forward 30, :evening
association :venue, factory: :venue
end
end
FactoryGirl.define do
factory :venue do
name Faker::Address.street_name
end
end
我试图通过传递有效属性来测试我的shows_controller
,我遇到的问题是FactoryGirl的attributes_for
方法没有返回venue_id
字段。这是我运行puts attributes_for
{:show_date=>Tue, 03 Mar 2015, :doors_open=>2015-03-12 17:44:45 -0500, :dinner_starts=>2015-02-25 20:35:13 -0600, :dinner_ends=>2015-03-08 19:58:18 -0500, :show_starts=>2015-02-25 21:25:55 -0600, :show_ends=>2015-03-17 21:21:57 -0500}
我在这里设置错了吗?为什么我没有收到venue_id
?
答案 0 :(得分:0)
由于您试图让FactoryGirl通过对关联的引用生成外键,我想知道has_and_belongs_to_many
关联是否导致问题。看起来您在这里的关系是Venue
只是has_many
显示的; Venue
不依赖于(AKA“属于”)一个节目的存在(场地表中没有外观的表演)。看看更改关联是否清除了FactoryGirl的内容。