2016-02-15 36 views
1

我得到了與聯賽ActiveModel :: MissingAttributeError:不能寫未知屬性`user_id`?

create_table "leagues", force: :cascade do |t| 
    t.string "name",       null: false 
    t.date  "start_date",      null: false 
    t.date  "end_date" 
    t.string "day" 
    t.string "start_time",      null: false 
    t.integer "practice_length",    null: false 
    t.integer "alley_id",      null: false 
    t.integer "frequency",      null: false 
    t.integer "scratch",   default: 220 
    t.integer "handicap_percent", default: 80 
    t.integer "handicap_round", default: 0 
    t.integer "occurrences",     null: false 
    t.integer "user_id",      null: false 
    t.datetime "created_at",      null: false 
    t.datetime "updated_at",      null: false 
    end 

和模型

class Alley < ActiveRecord::Base 
    belongs_to :address 
    has_many :leagues 
end 

class User < ActiveRecord::Base 
    has_many :leagues 
end 

class League < ActiveRecord::Base 
    belongs_to :alley 
    belongs_to :user 
    has_many :teams 
end 

和工廠

factory :league do 
    name Faker::Company.name 
    start_date start_date 
    end_date end_date 
    day Date.today.day 
    start_time '7:00pm' 
    practice_length 10 
    frequency 1 
    occurrences weeks 

    association :alley, factory: :alley 
    association :user, factory: :user 
end 

factory :user do 
    email '[email protected]' 
    password 'Test1234' 
    password_confirmation { 'Test1234' } 
end 

factory :alley do 
    name Faker::Company.name 
    association :address, factory: :address 
end 

當我嘗試創建一個聯賽,我得到一個錯誤的模式

ActiveModel::MissingAttributeError: can't write unknown attribute user_id

它的設置就像衚衕一樣,衚衕也很有用。一個聯盟應該被分配到一個衚衕並分配給一個用戶。一條小巷可以有很多聯賽,一個用戶可以有很多聯賽。坦率地說,我太累了,不知道爲什麼這個目前不能工作。

+0

你能更新'user'的工廠嗎? –

+0

是的,我可以更新它,以什麼? – TIMBERings

+0

是的,我只是想看到它更清晰,順便說一句,你可以重新檢查'user_id'在你的架構中,'ActiveModel :: MissingAttributeError'表明像'user_id'丟失! –

回答

1

我一直在修改遷移(因爲我得到的初始數據集),我遷移了下來,然後向上。那是什麼讓我在我的地方。我不得不放棄數據庫,然後運行安裝程序。一些東西一定是陳舊的。

+0

想驗證你的假設。如果您創建沒有索引的遷移,請向前遷移,將索引(或引用)添加到遷移文件,遷移或回滾,再次向前遷移,就會搞砸了。 Rails團隊認爲這不是一個可以接受的用例,所以不要期望修復。 – IAmNaN

相關問題