I need to save a large number of posts
with a translated body
attribute, so I'm trying to use insert_all
/upsert_all
in conjunction with Mobility. Saving posts individually works just fine:
Post.create({
body_en: "Hello world",
body_ja: "ハロー・ワールド",
...
})
However, trying to use insert_all
results in an error since body
is not stored in the posts table, but rather in mobility_text_translations table related via a polymorphic association (I'm using the default key/value backend):
posts = []
# In a loop, add many posts to the posts array
post = {
body_en: "Hello world",
body_ja: "ハロー・ワールド".
...
}
posts << post
# Add posts to DB at some interval
Post.insert_all(posts)
# => unknown attribute 'body_en' for Post. (ActiveModel::UnknownAttributeError)
I think this is somewhat similar to ActionText, where instead of ActionText::RichText.insert_all(post_bodies)
I wonder if we could do something like Mobility::TextTranslations.insert_all(post_bodies)
. However, I don't see this capability discussed on the GitHub page nor in the issues.