使用区域设置按关联上的Globalize3转换对ActiveRecord查询响应排序

使用区域设置按关联上的Globalize3转换对ActiveRecord查询响应排序,activerecord,internationalization,translation,ruby-on-rails-3.2,globalize3,Activerecord,Internationalization,Translation,Ruby On Rails 3.2,Globalize3,我一共有两个模型和三个表格。不幸的是,已经从原始表名中对模型进行了一些重命名,请原谅这种混淆: Symptom (chief_complaints) SymptomName (chief_complaint_names) (chief_complaint_name_translations) 当然,Globalize3使用后者来转换SympleName模型的:name属性。症状有很多:症状名称 考虑以下几点: Symptom.includes(names: :translations).ord

我一共有两个模型和三个表格。不幸的是,已经从原始表名中对模型进行了一些重命名,请原谅这种混淆:

Symptom (chief_complaints)
SymptomName (chief_complaint_names)
(chief_complaint_name_translations)
当然,Globalize3使用后者来转换SympleName模型的:name属性。症状有很多:症状名称

考虑以下几点:

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”。如果我对SymphonName进行查询(包括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"
如何将区域设置正确地注入到第一个查询中,并让它根据关联的症状名称对症状列表进行排序。值得注意的是,我有一个关于Symptom的方法,它返回它遇到的第一个SymptomName

我需要它按从区域设置派生的名称对列表进行排序,并显示专有名称。有什么想法吗


谢谢大家!

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

不幸的是,这个新模型似乎并没有像人们所期望的那样直接访问它的同名者。没有症状名称::Translation.first.symptom`

对所有关联的反思确实揭示了从翻译模型到所述模型的关系,但该关联无法通过正常链获得


我们最终创建了自己的模型来访问名为
SymptomNameTranslation
的表,添加了适当的关联和联接作为默认的\u范围。似乎工作得很好,但如果能在gem中有这样的功能就好了。这可能是一个很好的机会,为上述gem提供补丁。

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

不幸的是,这个新模型似乎并没有像人们所期望的那样直接访问它的同名者。没有症状名称::Translation.first.symptom`

对所有关联的反思确实揭示了从翻译模型到所述模型的关系,但该关联无法通过正常链获得

我们最终创建了自己的模型来访问名为
SymptomNameTranslation
的表,添加了适当的关联和联接作为默认的\u范围。似乎工作得很好,但如果能在gem中有这样的功能就好了。可能是一个很好的机会,补丁说,宝石