2016-10-03 65 views
0

我对RoR比较新。 这工作得很好:Rails - collection_select不从具有翻译的表中选择值

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

这其中挑选没有价值(实际上翻译所有这些调用表):

<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 

Tull的表具有也是在单独的表中的翻译。

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仍然不显示正确的值。它显示翻译的值,但不从表中选择值。我刚刚意识到,值也不会被选择用于未翻译的表格。 –