2017-12-02 201 views
0

工廠女孩文檔建議使用ADD_ATTRIBUTE方法來處理屬於保留字的屬性,具體如下:工廠女孩保留字屬性的關聯

factory :dna do 
    add_attribute(:sequence) { 'GATTACA' } 
end 

但是我有一個名爲特質(這是保留的)模型稱爲ContestantTrait的另一種模型,屬於Trait。我試着用不同的方式使用add_attribute來避免關聯中的保留字問題,但似乎沒有任何工作。

有什麼建議嗎?這是到目前爲止,我已經試過格式:

FactoryGirl.define do 
    factory :contestant_trait do 
    add_attribute(:trait) 
    add_attribute(:trait) {} 
    add_attribute(:trait) {trait} 
    end 
end 
+0

你可以使用新名稱來保留保留字,你應該沒問題。 –

回答

0

沒有必要界定,因爲:trait:add_attribute是一個關聯。使用關鍵字association如下:

FactoryGirl.define do 
    factory :contestant_trait do 
    association :trait 
    # ... other attributes 
    end 
end 

作爲一個側面說明,建議升級到FactoryBot

由於兩個factory_girl和factory_girl_rails的4.9.0版本中,無論是寶石會正式棄用。在4.9.0版本中。

看到這個職位https://robots.thoughtbot.com/factory_bot更多的形成和如何升級。

+0

你能解決你的問題與上述解決方案? – s3tjan