2

I have two models and three tables total which are involved. Unfortunately, some renaming of the models from the original table names has occurred, so pardon the confusion.:

Symptom (chief_complaints)
SymptomName (chief_complaint_names)
(chief_complaint_name_translations)

The latter, of course, is used by Globalize3 to translate the :name attribute of the SymptomName model. Symptom has_many :symptom_names.

Consider the following:

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

This returns a mostly correct list, but it's sorting the list of Symptoms by the first translation name it encounters (despite my locale), and listing them by the correct locale.

# .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

You'll notice that there is no mention of 'locale' or 'en' in this query. If I do a query on SymptomName, including the with_translations method, passing the locale into it, it writes the query exactly as I expect it would.

>> 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"

How can I properly inject the locale into the first query and have it sort my list of Symptoms according to the associated SymptomNames. It is worth noting that I have a method on Symptom that returns the first SymptomName it encounters.

I need this to sort the list by names derived from the locale AND to show the proper names. Any thoughts?

Thank you!

slant
  • 905
  • 8
  • 15

1 Answers1

0

Just a small update for anyone else using this gem. I was surprised to discover that the gem actually creates a new model on the fly for each model that has a translation. This new model has the same name as the original, appended with ::Translation. So SymptomName would have a SymptomName::Translation model. This model has the translated attributes accessible, such as SymptomName::Translation.first.name.

Unfortunately, this new model does not seem to have direct access back to it's namesake as one might expect. There is no SymptomName::Translation.first.symptom`.

Reflecting on all associations does reveal a relationship to said model from the translation model, but the association is not available through the normal chain.

We ended up creating our own model to access this table called SymptomNameTranslation, adding the appropriate associations and joins as a default_scope. Seems to be working great, but would be nice to have this in the gem itself. Might be a good opportunity for a patch to said gem.

slant
  • 905
  • 8
  • 15