4

gobalizeRails 4.1.12 で gem 4.0.3を使用しています。

モデルがあり、テーブルを設定するために提供された移行Postを実行しました。Post.create_translation_table!globalizepost_translations

ここで、フィクスチャ ファイルから翻訳を自動的にロードしたいと考えています。フィクスチャは関連付けのラベル参照をサポートしているため、次のようにしています。

# spec/fixtures/posts.yml

my_first_post:
  author: dave

# spec/fixtures/post_translations.yml

my_first_post_translation:
  locale: en    
  title: My first post
  content: What a time to be alive!
  post: my_first_post

# spec/models/post_spec

require 'rails_helper'

RSpec.describe Post, type: :model do
  fixtures('post/translations', :posts, :authors)

  subject(:post) { posts(:my_first_post) }

  it "has an author" do
    expect(post.author).to eq(authors(:dave))
  end

  it "has a title" do
    binding.pry
    expect(post.title).to be_present
  end
end

しかし、RSpec を実行すると、次のエラーがスローされます。

 Failure/Error: Unable to find matching line from backtrace
 ActiveRecord::StatementInvalid:
   SQLite3::SQLException: table post_translations has no column named post: INSERT INTO "post_translations" ("locale", "title", "content", "post", "created_at", "updated_at", "id") VALUES ('en', 'My first post', 'This is some compelling english content', 'my_first_post', '2015-08-21 10:23:27', '2015-08-21 10:23:27', 626768522)

逆を行うと同様のエラーが発生します(つまりpost_translation: my_first_translationposts.yml

どうすれば魔法を取り戻すことができますか?

4

1 に答える 1

14

解決

  1. fixtures/post_translations.ymlに移動fixtures/post/translations.yml
  2. の関連付けのキーmy_first_translationglobalized_model

したがって、次のようになります。

<!-- language: yml -->
# spec/fixtures/posts.yml

my_first_post:
  author: dave

# spec/fixtures/post/translations.yml

my_first_post_translation:
  locale: en
  title: My first post
  content: What a time to be alive!
  globalized_model: my_first_post

説明

  1. 翻訳モデルはPost::Translation. 名前空間は、Post::Translationフィクスチャの位置が のモデルと同じネスト規則に従う必要があることを意味しapp/modelsます。

  2. Post::Translation(および任意のglobalize変換モデル)の関連付け名はglobalized_modelです。ActiveRecord::FixtureSet 関連付け名を使用して、フィクスチャ キーを関連付けとして認識します

于 2015-08-21T10:37:36.870 に答える