2014-09-12 62 views
1

我當前已定義了一個簡單的工廠爲:工廠女孩使用多個替代屬性

factory :tm_event do 
    ... 
    category "MyString" 
    category_id { Random.rand(1..2147483647) } 
    ... 
end 

我試圖創建一個具有以下一個TmEvent對象:

FactoryGirl.create(:tm_event, category: 'rock', category_id: '12') 

當此同時運行類別和category_id在結果對象中設置爲nil

如果我單獨運行category或category_id設置,那麼兩個實例都會被設置爲覆蓋相應的屬性。當我直接創建事件TmEvent.create(category: 'rock', category_id:'12')時,這兩個屬性都被填充。

我的語法正確嗎? 我應該在哪裏尋找解決方案?

+1

什麼是'tm_event'?什麼是'類別'?你能分享這個模型嗎?我認爲你要做的是與'tm_event'一起創建一個類別。 – 2014-09-12 11:58:42

+0

@RyanBigg tm_event是一個ActiveRecord模型對象,其中category是一個字符串,category_id是一個整數。在create方法中將id字符串更改爲整數也沒有區別。 – Richbits 2014-09-12 12:20:47

回答

1

你的語法看起來不錯。我只是在我的控制檯中嘗試過。它更新了這兩個屬性。

2.1.2 :010 > FactoryGirl.build(:user) 
=> #<User id: nil, email: "[email protected]", encrypted_password: "$2a$10$WnXKcpKZt0kkkXyRRe/QP.NS7mIaFcgFzCpZhW0hzEO...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: nil, updated_at: nil, god: nil, name: "Test User 123", sash_id: nil, level: 0, confirmation_token: nil, confirmed_at: "2014-09-12 11:58:28", confirmation_sent_at: nil, unconfirmed_email: nil, failed_attempts: 0, unlock_token: nil, locked_at: nil> 
2.1.2 :011 > FactoryGirl.build(:user, email: '123', sign_in_count: 2) 
=> #<User id: nil, email: "123", encrypted_password: "$2a$10$JIjhQp40Lz/2fCskW63lzOHHxV2NwX2h5URswbuZsgH...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 2, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: nil, updated_at: nil, god: nil, name: "Test User 123", sash_id: nil, level: 0, confirmation_token: nil, confirmed_at: "2014-09-12 11:58:28", confirmation_sent_at: nil, unconfirmed_email: nil, failed_attempts: 0, unlock_token: nil, locked_at: nil> 
2.1.2 :012 > 

嘗試在您的控制檯。

+0

謝謝,沒想到這是錯誤的語法,因爲我在其他地方使用它,雖然有不同的類型。 – Richbits 2014-09-12 12:29:13