2016-10-03 5 views
0

私はRoRが比較的新しいです。 これはうまく動作します:Rails - collection_selectは変換テーブルを持つテーブルから値を選択しません

<td><%= collection_select :competitions_members, :member_id, Member.all, :id, :first_name %></td> 

この1が値(翻訳付きのテーブルに、実際にそのようなすべてのコールを)選ぶん:

<td><%= collection_select :competitions_members, :tull_id, Tull.all, :id, :name %></td> 

seeded data in competitions_members table

メンバーは、多くの競争に関与することができます。基本的に、私は競技会メンバーテーブルを介してメンバーと競技会の間にN:M関係を持っています。 Tullは辞書です。競技会に会員を割り当てる過程で設定される値。

データモデルクラス:

class Member < ApplicationRecord 
    has_and_belongs_to_many :competitions 
end 

class Competition < ApplicationRecord 
    has_and_belongs_to_many :members 
end 

class CompetitionsMember < ApplicationRecord 
end 

タルテーブルも別のテーブルの翻訳があります。

class Tull < ApplicationRecord 
    translates :name 
    has_many :competitions_members 

    # separate different localizations of the record 
    def cache_key 
    super + '-' + Globalize.locale.to_s 
    end 
end 

関連schema.db抜粋

create_table "members", force: :cascade do |t| 
    t.string "first_name" 
    t.string "last_name" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    end 
    create_table "competitions", force: :cascade do |t| 
    t.string "name" 
    t.datetime "created_at",  null: false 
    t.datetime "updated_at",  null: false 
    end 
    create_table "competitions_members", force: :cascade do |t| 
    t.integer "member_id" 
    t.integer "competition_id" 
    t.datetime "created_at",  null: false 
    t.datetime "updated_at",  null: false 
    t.integer "tull_id" 
    t.index ["tull_id"], name: "index_competitions_members_on_tull_id" 
    end 
    create_table "tull_translations", force: :cascade do |t| 
    t.integer "tull_id", null: false 
    t.string "locale",  null: false 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    t.string "name" 
    t.index ["locale"], name: "index_tull_translations_on_locale" 
    t.index ["tull_id"], name: "index_tull_translations_on_tull_id" 
    end 
    create_table "tulls", force: :cascade do |t| 
    t.string "name" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    end 

すべてのヘルプapreciated。私はちょうどこれが翻訳されたテーブルと何らかの形で結びついているかもしれないことに気づいた

+0

Rails 5 ActiveRecord/AppplicationRecordに関連する 'translates'指示文への参照が見つかりません。他の場所から来ていますか? – Eric

+0

私は 'gem 'globalize'、github: 'globalize/globalize''を使っています。詳細はこちら - https://github.com/globalize/globalize –

答えて

0
class Tull < ApplicationRecord  
    has_many :translations 
    translates :name, :fallbacks_for_empty_translations => true 
    attr_accessible :translations_attributes 
end 

レールコンソール内のコードの下に実行するようにしてください:

Tull.first.translations - これはあなたの翻訳レコードを与えた場合は団体が正しいことを意味します。

ここで、ビュー側で確認して、多言語用の属性をどのように生成しますか。 globalize_accessorsを使用することをお勧めします。

私にコードベースを送ってください。

+0

ありがとうございます。私はクラスを更新し、コンソールを介してテストし、関連は正しい - https://github.com/valasek/taekwondo/blob/master/app/models/tull.rb。しかし、collection_selectは正しい値を表示しません。変換された値は表示されますが、テーブルの値は選択されません。私は、変換されていないテーブルに対しても値が選択されていないことに気付きました。 –

関連する問題