我正在使用 Globalize 3 gem,如 Ryan Bates Railscasts 中所示,一切正常。我需要知道如何播种数据。目前,我创建了一个名为 Monthly_post_translations 的表,其架构如下
架构.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,它不起作用
种子.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
关于如何正确添加此种子数据有什么想法吗?
最佳答案
自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" )
关于ruby-on-rails - 播种没有模型的翻译表,rails 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10066202/