Rails - collection_select不会从具有翻译的表中选择值

时间:2016-10-03 18:25:19

标签: ruby-on-rails globalize

我对RoR比较陌生。 这很好用:

<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

会员可参与许多比赛。基本上我通过competitions_members表在成员和比赛之间建立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

任何有帮助的帮助。我刚才意识到这可能会以某种方式与翻译表联系起来。

1 个答案:

答案 0 :(得分:0)

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

尝试在rails console中执行以下代码:

Tull.first.translations - 如果这为您提供翻译记录,则表示关联正确。

现在查看视图,如何为多语言内容生成属性。我建议使用globalize_accessors

请把代码库发给我。