ruby-on-rails - 播种没有模型的翻译表,rails 应用程序

标签 ruby-on-rails seed globalize3

我正在使用 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/

相关文章:

c - 需要帮助修复段错误

ruby-on-rails-4 - Rails 4 上的 gem globalize3

ruby-on-rails - ruby on rails 3 使用缓存数据重新加载

ruby-on-rails - 最稳定的 memcache ruby​​ gem 是什么?

php - 在 Laravel 中,如何从 Users 表中检索随机 user_id 以生成模型工厂种子数据?

ruby-on-rails - Globalize3 - 尝试将翻译添加到现有模型时出现迁移错误

mysql - 用一个表中的引用 ID 替换另一个表中的字段

java - 学习 Ruby on Rails(已获得面向对象的知识)

python - 'random' 的独立实例