2012-04-08 16 views
2

我使用Ryan Bates railscasts中看到的Globalize 3 gem,並且一切正常。我需要知道如何種子數據。目前,我創建了一個名爲下面的模式monthly_post_translations種子沒有模型的轉換表,軌應用程序

schema.rb

create_table "monthly_post_translations", :force => true do |t| 
    t.integer "monthly_post_id" 
    t.string "locale" 
    t.text  "body" 
    t.datetime "created_at" 
    t.datetime "updated_at" 
end 

我需要的種子數據添加到該表的表,但沒有一個模型進行交互,所以如何我要做嗎?

這裏是我的電流seeds.rb不工作

seeds.rb

# Monthly Posts 
    MonthlyPost.delete_all 

monthlypost = MonthlyPost.create(:body => "Monthly Post Text") 


#Monthly Posts Spanish Translation 
monthlytranslation = MonthlyPostTranslation.create(:body => "Spanish Translation of monthly post text", 
     :monthly_post_id => monthlypost.id, 
     :locale => "es") 

但monthly_post_translation表沒有,我可以交互的模式,所以我得到的錯誤

uninitialized constant MonthlyPostTranslation 

有關如何正確添加此種子數據的任何想法?

回答

4

documentation通過輸入translates :<attribute_name_here>您會生成名爲MonthlyPost::Translation的模型。所以答案將是:使用實例集合來創建或列出實體的所有翻譯:

monthlypost = MonthlyPost.create(:body => "Monthly Post Text") 


#Monthly Posts Spanish Translation 
monthlytranslation = monthlypost.translations.create(:body => "Spanish Translation of monthly post text", 
     :locale => "es") 
+0

確實有效,非常感謝。 1個問題,但是這個MonthlyPost :: Translation模型在我的rails應用程序中駐留了哪些內容? – ruevaughn 2012-04-08 21:45:19

+0

當'MonthlyPost'類被加載時爲您生成。確切的位置在'translates'方法下:[source](https://github.com/svenfuchs/globalize3/blob/master/lib/globalize/active_record/class_methods.rb#L41) – 2012-04-09 05:44:27