2

I am using the Globalize 3 gem as seen in Ryan Bates railscasts, and everything works fine. I need to know how to seed the data though. Currently, I created a table called monthly_post_translations with the following schema

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

I need to add seed data to this table, but it doesn't have a model to interact with, so how do I do it?

Here is my currents seeds.rb that isn't working

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" )

But the monthly_post_translation table doesn't have a model that I can interact with, so I get the error

uninitialized constant MonthlyPostTranslation

Any thoughts on how I can add this seed data properly?

ruevaughn
  • 1,319
  • 1
  • 17
  • 48

1 Answers1

4

As from documentation by typing translates :<attribute_name_here> you get generated model named MonthlyPost::Translation. So the answer will be: use instance collection to create or list all translations for entity:

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


#Monthly Posts Spanish Translation
monthlytranslation = monthlypost.translations.create(:body => "Spanish Translation of monthly post text",
      :locale => "es" )
Mark Huk
  • 2,379
  • 21
  • 28
  • That indeed worked, thanks so much. 1 question though, where does this MonthlyPost::Translation model reside in my rails app? – ruevaughn Apr 08 '12 at 21:45
  • It is generated for you when `MonthlyPost` class is loaded. The exact place is under `translates` method: [source](https://github.com/svenfuchs/globalize3/blob/master/lib/globalize/active_record/class_methods.rb#L41) – Mark Huk Apr 09 '12 at 05:44