2

我总共有两个模型和三个表。不幸的是,模型从原始表名发生了一些重命名,所以请原谅混乱。:

Symptom (chief_complaints)
SymptomName (chief_complaint_names)
(chief_complaint_name_translations)

后者当然被 Globalize3 用来转换 SymptomName 模型的 :name 属性。症状 has_many :symptom_names。

考虑以下:

Symptom.includes(names: :translations).order("chief_complaint_name_translations.name ASC")

这会返回一个基本正确的列表,但它会按遇到的第一个翻译名称(尽管我的语言环境)对症状列表进行排序,并按正确的语言环境列出它们。

# .to_sql output
"SELECT `chief_complaints`.* FROM `chief_complaints` INNER JOIN `chief_complaint_names` ON `chief_complaint_names`.`chief_complaint_id` = `chief_complaints`.`id` INNER JOIN `chief_complaint_name_translations` ON `chief_complaint_names`.`id` = `chief_complaint_name_translations`.`chief_complaint_name_id` ORDER BY chief_complaint_name_translations.name, chief_complaint_name_translations.name ASC"

# Actual SQL generated in the console
SELECT `chief_complaints`.* FROM `chief_complaints` INNER JOIN `chief_complaint_names` ON `chief_complaint_names`.`chief_complaint_id` = `chief_complaints`.`id` INNER JOIN `chief_complaint_name_translations` ON `chief_complaint_names`.`id` = `chief_complaint_name_translations`.`chief_complaint_name_id` ORDER BY chief_complaint_name_translations.name, chief_complaint_name_translations.name ASC
SELECT `chief_complaint_names`.`id` AS t0_r0, `chief_complaint_names`.`chief_complaint_id` AS t0_r1, `chief_complaint_names`.`created_at` AS t0_r2, `chief_complaint_names`.`updated_at` AS t0_r3, `chief_complaint_name_translations`.`id` AS t1_r0, `chief_complaint_name_translations`.`chief_complaint_name_id` AS t1_r1, `chief_complaint_name_translations`.`locale` AS t1_r2, `chief_complaint_name_translations`.`name` AS t1_r3, `chief_complaint_name_translations`.`created_at` AS t1_r4, `chief_complaint_name_translations`.`updated_at` AS t1_r5, `chief_complaint_name_translations`.`url` AS t1_r6 FROM `chief_complaint_names` LEFT OUTER JOIN `chief_complaint_name_translations` ON `chief_complaint_name_translations`.`chief_complaint_name_id` = `chief_complaint_names`.`id` WHERE `chief_complaint_name_translations`.`locale` = 'en' AND `chief_complaint_names`.`chief_complaint_id` IN (173, 2, 1, 224, 223, 3, 75, 4, 186, 15, 199, 201, 5, 177, 245, 94, 219, 225, 241, 6, 228, 213, 234, 164, 88, 26, 81, 7, 74, 136, 57, 21, 28, 18, 163, 165, 8, 112, 183, 147, 9, 160, 10, 64, 218, 170, 200, 207, 11, 175, 13, 138, 72, 12, 214, 239, 248, 14, 150, 190, 137, 16, 17, 154, 178, 127, 56, 206, 246, 101, 19, 20, 22, 96, 172, 255, 23, 24, 216, 25, 215, 29, 125, 113, 198, 195, 244, 27, 247, 132, 232, 70, 135, 133, 30, 31, 34, 32, 197, 181, 222, 208, 243, 35, 227, 196, 33, 36, 179, 53, 131, 126, 159, 58, 37, 202, 203, 38, 120, 68, 220, 230, 176, 39, 226, 148, 174, 91, 40, 41, 145, 151, 134, 189, 73, 43, 42, 47, 93, 44, 45, 46, 209, 192, 204, 205, 48, 188, 128, 49, 212, 249, 250, 211, 153, 50, 51, 52, 139, 187, 237, 109, 156, 129, 54, 157, 55, 87, 69, 84, 146, 60, 149, 221, 231, 242, 229, 59, 194, 240, 155, 61, 158, 62, 171, 180, 67, 63, 236, 65, 66, 162, 71, 152, 191, 76, 77, 78, 79, 80, 82, 83, 103, 92, 98, 118, 85, 100, 89, 116, 114, 115, 104, 99, 111, 86, 97, 122, 251, 90, 238, 193, 254, 95, 252, 130, 235, 233, 102, 121, 123, 105, 106, 107, 108, 110, 217) ORDER BY name

您会注意到此查询中没有提及“locale”或“en”。如果我对 SymptomName 进行查询,包括 with_translations 方法,将语言环境传递给它,它会完全按照我的预期编写查询。

>> SymptomName.with_translations(I18n.locale).to_sql
=> "SELECT `chief_complaint_names`.* FROM `chief_complaint_names` WHERE `chief_complaint_name_translations`.`locale` = 'en' ORDER BY name"

如何正确地将语言环境注入到第一个查询中,并让它根据关联的 SymptomNames 对我的症状列表进行排序。值得注意的是,我在 Symptom 上有一个方法,它返回它遇到的第一个 SymptomName。

我需要这个来按从语言环境派生的名称对列表进行排序,并显示正确的名称。有什么想法吗?

谢谢!

4

1 回答 1

0

对使用此 gem 的其他人来说只是一个小更新。我惊讶地发现 gem 实际上为每个具有翻译的模型创建了一个新模型。这个新模型与原始模型具有相同的名称,并附加了::Translation. 所以SymptomName会有一个SymptomName::Translation模型。此模型具有可访问的已翻译属性,例如SymptomName::Translation.first.name.

不幸的是,这种新模型似乎无法像人们预期的那样直接访问它的同名模型。没有 SymptomName::Translation.first.symptom`。

反映所有关联确实揭示了从翻译模型到所述模型的关系,但是该关联不能通过正常链获得。

我们最终创建了自己的模型来访问这个名为 的表SymptomNameTranslation,并将适当的关联和连接添加为 default_scope。似乎工作得很好,但是在宝石本身中有这个会很好。可能是修补所述宝石的好机会。

于 2012-07-05T18:25:46.657 回答