Converting UTF-8 iso-8859 bytes - String.to_charlist - utf-8

I have run into an odd problem. I have a program that receives data in bytes from an external source, which I in return perform some business logic and send a reply. We have come into an odd issue with String.to_charlist.
Try this in iex:
String.to_charlist <<169, 99, 111, 114, 94, 51, 94, 51, 94, 66, 52, 57, 49, 49, 49, 32, 32, 49, 48, 51, 53, 94, 67, 79, 77, 80, 76, 69, 84, 69, 68, 94, 49, 49, 52, 50, 52, 53, 94, 75>>
This will throw the following error:
** (UnicodeConversionError) invalid encoding starting at <<169, 99, 111, 114, 94, 51, 94, 51, 94, 66, 52, 57, 49, 49, 49, 32, 32, 49, 48, 51, 53, 94, 67, 79, 77, 80, 76, 69, 84, 69, 68, 94, 49, 49, 52, 50, 52, 53, 94, 75>>
(elixir) lib/string.ex:2035: String.to_charlist/1
Now if you strip off the first byte and in turn try:
String.to_charlist <<99, 111, 114, 94, 51, 94, 51, 94, 66, 52, 57, 49, 49, 49, 32, 32, 49, 48, 51, 53, 94, 67, 79, 77, 80, 76, 69, 84, 69, 68, 94, 49, 49, 52, 50, 52, 53, 94, 75>>
You get:
'cor^3^3^B49111 1035^COMPLETED^114245^K'
Is there a different way I should be converting these bytes to a string? I understand some characters like 169 may not be displayable, but what is the recommended way of handling such?
Thanks for your help.
I did find this:
<<169 :: utf8, 0>>
Returns <<194, 169, 0>> which added the 194. So if you paste this into iex the output appears to be correct.
<<194, 169, 99, 111, 114, 94, 51, 94, 51, 94, 66, 52, 57, 49, 49, 49, 32, 32, 49, 48, 51, 53, 94, 67, 79, 77, 80, 76, 69, 84, 69, 68, 94, 49, 49, 52, 50, 52, 53, 94, 75>>
Do I need to write a function that loops through the bytes and calls <> then reduces on the bytes returned (except for the 0 concat)?

String.to_charlist/1 is redundant in the first place:
String.to_charlist <<99, 111, 114, 94, 51, 94, 51, 94>>
works only because utf8 and latin1 share the same codepoints in the interval 1–127. The below would be enough to get a perfectly valid binary:
<<99, 111, 114, 94, 51, 94, 51, 94>>
#⇒ "cor^3^3^"
Unfortunately, what you receive is not in utf8 encoding and Elixir has no built-in tools to convert binaries between encoding. You cannot just drop meaningful symbols.
I would suggest using codepagex package for that conversion:
Codepagex.from_string(<<99, 111, 114, 94, 51, 94, 51, 94>>, :iso_8859_1)
#⇒ "cor^3^3^"
Other way round would be to use erlang’s unicode.characters_to_binary/2:
:unicode.characters_to_binary(
<<169, 99, 111, 114, 94, 51, 94, 51, 94>>, :latin1, :utf8
)
#⇒ "©cor^3^3^"
Another solution using Kernel.SpecialForms.for/1 comprehension:
to_string(for <<c :: 8 <- <<169, 99, 111, 114, 94, 51, 94, 51, 94>> >>, do: c)
#⇒ "©cor^3^3^"

Related

Contract-helper url for mainnet: to get delegation info of specific account id

I want to get delegation information(validators, amount) of specific account id. This link, https://near-contract-helper.onrender.com/staking-deposits/kms.testnet, seems like for testnet because I could find delegation information of my testnet account.
however, I couldn’t get results with my mainnet account (I delegated 1.36NEAR for sure): https://near-contract-helper.onrender.com/staking-deposits/kwklly.near
So I’m wondering if there’s any other url for mainnet to get delegation information of specific account id.
++ there's a problem in testnet contract-helper: FAILED TXS are shown either.
I failed to delegate to #dsrvlabs.poolv1.near, but I could see it with contract-helper.
Tx receipt: https://explorer.testnet.near.org/transactions/7LFsSHGQ5WgcVDWwgrwYjD38umb5QHrdYsVMFjDadngH
contract-helper:
https://near-contract-helper.onrender.com/staking-deposits/kms.testnet
The Contract Helper at that address is configured to query testnet instead of mainnet, it can't work on both. The correct URL is https://helper.mainnet.near.org/
However, I was able to retrieve the same information with Near-CLI, using the following command:
near view dsrvlabs.poolv1.near get_account '{"account_id": "kwklly.near"}'
The result is:
$ near view dsrvlabs.poolv1.near get_account '{"account_id": "kwklly.near"}'
View call: dsrvlabs.poolv1.near.get_account({"account_id": "kwklly.near"})
{
account_id: 'kwklly.near',
unstaked_balance: '1',
staked_balance: '1370597785335174013176165',
can_withdraw: true
}
This information can be retrieved even without Near-CLI or the contract helper, querying a NEAR node running inside your own network. An example in cURL for the very same call is:
curl --location --request POST 'https://rpc.mainnet.near.org/' \
--header 'Content-Type: application/json' \
--data-raw '{
"jsonrpc": "2.0",
"method": "query",
"params": {
"request_type": "call_function",
"finality": "final",
"account_id": "dsrvlabs.poolv1.near",
"method_name": "get_account",
"args_base64": "eyJhY2NvdW50X2lkIjogImt3a2xseS5uZWFyIn0="
},
"id": "dontcare"
}'
The expected result should be:
{
"jsonrpc": "2.0",
"result": {
"block_hash": "5MCrRZAgYzFdYMpHXeQJ25DjrRVFQ1KVgyy8qYTvUhZi",
"block_height": 44877555,
"logs": [],
"result": [
123,
34,
97,
99,
99,
111,
117,
110,
116,
95,
105,
100,
34,
58,
34,
107,
119,
107,
108,
108,
121,
46,
110,
101,
97,
114,
34,
44,
34,
117,
110,
115,
116,
97,
107,
101,
100,
95,
98,
97,
108,
97,
110,
99,
101,
34,
58,
34,
49,
34,
44,
34,
115,
116,
97,
107,
101,
100,
95,
98,
97,
108,
97,
110,
99,
101,
34,
58,
34,
49,
51,
55,
48,
53,
57,
55,
55,
56,
53,
51,
51,
53,
49,
55,
52,
48,
49,
51,
49,
55,
54,
49,
54,
53,
34,
44,
34,
99,
97,
110,
95,
119,
105,
116,
104,
100,
114,
97,
119,
34,
58,
116,
114,
117,
101,125
]
},
"id": "dontcare"
}
Three important elements to know if you build your own parser:
the argument is base64 encoded, such that {"account_id": "kwklly.near"} from Near-cli becomes "args_base64": "eyJhY2NvdW50X2lkIjogImt3a2xseS5uZWFyIn0="
the result is an ascii array, such that 123 is { and the sequence 123 34 97 99 99 111 117 110 116 95 105 100 34 58 is the beginning of the result {"account_id":
you can POST the same query to your local node (even the IP address), changing https://rpc.mainnet.near.org/ with http://10.0.0.10:3030/

i am trying to use DeadLetterPublishingRecoverer but not working

2020-04-28 18:49:05.388 ERROR 14531 --- [ntainer#0-0-C-1] o.s.k.l.DeadLetterPublishingRecoverer : Dead-letter publication failed for: ProducerRecord(topic=LP_EVENT.DLT, partition=4, headers=RecordHeaders(headers = [RecordHeader(key = kafka_dlt-original-topic, value = [76, 80, 95, 69, 86, 69, 78, 84]), RecordHeader(key = kafka_dlt-original-partition, value = [0, 0, 0, 4]), RecordHeader(key = kafka_dlt-original-offset, value = [0, 0, 0, 0, 0, 0, 0, 1]), RecordHeader(key = kafka_dlt-original-timestamp, value = [0, 0, 1, 113, -66, -64, -61, 43]), RecordHeader(key = kafka_dlt-original-timestamp-type, value = [67, 114, 101, 97, 116, 101, 84, 105, 109, 101]), RecordHeader(key = kafka_dlt-exception-fqcn, value = [111, 114, 103, 46, 115, 112, 114, 105, 110, 103, 102, 114, 97, 109, 101, 119, 111, 114, 107, 46, 107, 97, 102, 107, 97, 46, 108, 105, 115, 116, 101, 110, 101, 114, 46, 76, 105, 115, 116, 101, 110, 101, 114, 69, 120, 101, 99, 117, 116, 105, 111, 110, 70, 97, 105, 108, 101, 100, 69, 120, 99, 101, 112, 116, 105, 111, 110]), RecordHeader(key = kafka_dlt-exception-message, value = [76, 105, 115, 116, 101, 110, 101, 114, 32, 109, 101, 116, 104, 111, 100, 32, 39, 112, 117, 98, 108, 105, 99, 32, 118, 111, 105, 100, 32, 111, 114, 103, 46, 120, 109, 46, 108, 112, 46, 101, 118, 101, 110, 116, 46, 97, 112, 105, 46, 108, 105, 115, 116, 101, 110, 101, 114, 46, 69, 118, 101, 110, 116, 76, 105, 115, 116, 101, 110, 101, 114, 46, 112, 114, 111, 99, 101, 115, 115, 69, 118, 101, 110, 116, 40, 111, 114, 103, 46, 120, 109, 46, 108, 112, 46, 101, 118, 101, 110, 116, 46, 99, 111, 109, 109, 111, 110, 46, 109, 111, 100, 101, 108, 46, 118, 111, 46, 69, 118, 101, 110, 116, 80, 97, 121, 108, 111, 97, 100, 44, 111, 114, 103, 46, 115, 112, 114, 105, 110, 103, 102, 114, 97, 109, 101, 119, 111, 114, 107, 46, 107, 97, 102, 107, 97, 46, 115, 117, 112, 112, 111, 114, 116, 46, 65, 99, 107, 110, 111, 119, 108, 101, 100, 103, 109, 101, 110, 116, 41, 32, 116, 104, 114, 111, 119, 115, 32, 106, 97, 118, 97, 46, 108, 97, 110, 103, 46, 69, 120, 99, 101, 112, 116, 105, 111, 110, 39, 32, 116, 104, 114, 101, 119, 32, 101, 120, 99, 101, 112, 116, 105, 111, 110, 59, 32, 110, 101, 115, 116, 101, 100, 32, 101, 120, 99, 101, 112, 116, 105, 111, 110, 32, 105, 115, 32, 106, 97, 118, 97, 46, 108, 97, 110, 103, 46, 82, 117, 110, 116, 105, 109, 101, 69, 120, 99, 101, 112, 116, 105, 111, 110, 58, 32, 102, 97, 105, 108, 101, 100, 59, 32, 110, 101, 115, 116, 101, 100, 32, 101, 120, 99, 101, 112, 116, 105, 111, 110, 32, 105, 115, 32, 106, 97, 118, 97, 46, 108, 97, 110, 103, 46, 82, 117, 110, 116, 105, 109, 101, 69, 120, 99, 101, 112, 116, 105, 111, 110, 58, 32, 102, 97, 105, 108, 101, 100]), RecordHeader(key = kafka_dlt-exception-stacktrace, value = [111, 114, 103, 46, 115, 112, 114, 105, 110, 103, 102, 114, 97, 109, 101, 119, 111, 114, 107, 46, 107, 97, 102, 107, 97, 46, 108, 105, 115, 116, 101, 110, 101, 114, 46, 76, 105, 115, 116, 101, 110, 101, 114, 69, 120, 101, 99, 117, 116, 105, 111, 110, 70, 97, 105, 108, 101, 100, 69, 120, 99, 101, 112, 116, 105, 111, 110, 58, 32, 76, 105, 115, 116, 101, 110, 101, 114, 32, 109, 101, 116, 104, 111, 100, 32, 39, 112, 117, 98, 108, 105, 99, 32, 118, 111, 105, 100, 32, 111, 114, 103, 46, 120, 109, 46, 108, 112, 46, 101, 118, 101, 110, 116, 46, 97, 112, 105, 46, 108, 105, 115, 116, 101, 110, 101, 114, 46, 69, 118, 101, 110, 116, 76, 105, 115, 116, 101, 110, 101, 114, 46, 112, 114, 111, 99, 101, 115, 115, 69, 118, 101, 110, 116, 40, 111, 114, 103, 46, 120, 109, 46, 108, 112, 46, 101, 118, 101, 110, 116, 46, 99, 111, 109, 109, 111, 110, 46, 109, 111, 100, 101, 108, 46, 118, 111, 46, 69, 118, 101, 110, 116, 80, 97, 121, 108, 111, 97, 100, 44, 111, 114, 103, 46, 115, 112, 114, 105, 110, 103, 102, 114, 97, 109, 101, 119, 111, 114, 107, 46, 107, 97, 102, 107, 97, 46, 115, 117, 112, 112, 111, 114, 116, 46, 65, 99, 107, 110, 111, 119, 108, 101, 100, 103, 109, 101, 110, 116, 41, 32, 116, 104, 114, 111, 119, 115, 32, 106, 97, 118, 97, 46, 108, 97, 110, 103, 46, 69, 120, 99, 101, 112, 116, 105, 111, 110, 39, 32, 116, 104, 114, 101, 119, 32, 101, 120, 99, 101, 112, 116, 105, 111, 110, 59, 32, 110, 101, 115, 116, 101, 100, 32, 101, 120, 99, 101, 112, 116, 105, 111, 110, 32, 105, 115, 32, 106, 97, 118, 97, 46, 108, 97, 110, 103, 46, 82, 117, 110, 116, 105, 109, 101, 69, 120, 99, 101, 112, 116, 105, 111, 110, 58, 32, 102, 97, 105, 108, 101, 100, 59, 32, 110, 101, 115, 116, 101, 100, 32, 101, 120, 99, 101, 112, 116, 105, 111, 110, 32, 105, 115, 32, 106, 97, 118, 97, 46, 108, 97, 110, 103, 46, 82, 117, 110, 116, 105, 109, 101, 69, 120, 99, 101, 112, 116, 105, 111, 110, 58, 32, 102, 97, 105, 108, 101, 100, 10, 9, 97, 116, 32, 111, 114, 103, 46, 115, 112, 114, 105, 110, 103, 102, 114, 97, 109, 101, 119, 111, 114, 107, 46, 107, 97, 102, 107, 97, 46, 108, 105, 115, 116, 101, 110, 101, 114, 46, 75, 97, 102, 107, 97, 77, 101, 115, 115, 97, 103, 101, 76, 105, 115, 116, 101, 110, 101, 114, 67, 111, 110, 116, 97, 105, 110, 101, 114, 36, 76, 105, 115, 116, 101, 110, 101, 114, 67, 111, 110, 115, 117, 109, 101, 114, 46, 100, 101, 99, 111, 114, 97, 116, 101, 69, 120, 99, 101, 112, 116, 105, 111, 110, 40, 75, 97, 102, 107, 97, 77, 101, 115, 115, 97, 103, 101, 76, 105, 115, 116, 101, 110, 101, 114, 67, 111, 110, 116, 97, 105, 110, 101, 114, 46, 106, 97, 118, 97, 58, 49, 55, 49, 54, 41, 10, 9, 97, 116, 32, 111, 114, 103, 46, 115, 112, 114, 105, 110, 103, 102, 114, 97, 109, 101, 119, 111, 114, 107, 46, 107, 97, 102, 107, 97, 46, 108, 105, 115, 116, 101, 110, 101, 114, 46, 75, 97, 102, 107, 97, 77, 101, 115, 115, 97, 103, 101, 76, 105, 115, 116, 101, 110, 101, 114, 67, 111, 110, 116, 97, 105, 110, 101, 114, 36, 76, 105, 115, 116, 101, 110, 101, 114, 67, 111, 110, 115, 117, 109, 101, 114, 46, 105, 110, 118, 111, 107, 101, 69, 114, 114, 111, 114, 72, 97, 110, 100, 108, 101, 114, 40, 75, 97, 102, 107, 97, 77, 101, 115, 115, 97, 103, 101, 76, 105, 115, 116, 101, 110, 101, 114, 67, 111, 110, 116, 97, 105, 110, 101, 114, 46, 106, 97, 118, 97, 58, 49, 55, 48, 52, 41, 10, 9, 97, 116, 32, 111, 114, 103, 46, 115, 112, 114, 105, 110, 103, 102, 114, 97, 109, 101, 119, 111, 114, 107, 46, 107, 97, 102, 107, 97, 46, 108, 105, 115, 116, 101, 110, 101, 114, 46, 75, 97, 102, 107, 97, 77, 101, 115, 115, 97, 103, 101, 76, 105, 115, 116, 101, 110, 101, 114, 67, 111, 110, 116, 97, 105, 110, 101, 114, 36, 76, 105, 115, 116, 101, 110, 101, 114, 67, 111, 110, 115, 117, 109, 101, 114, 46, 100, 111, 73, 110, 118, 111, 107, 101, 82, 101, 99, 111, 114, 100, 76, 105, 115, 116, 101, 110, 101, 114, 40, 75, 97, 102, 107, 97, 77, 101, 115, 115, 97, 103, 101, 76, 105, 115, 116, 101, 110, 101, 114, 67, 111, 110, 116, 97, 105, 110, 101, 114, 46, 106, 97, 118, 97, 58, 49, 54, 50, 49, 41, 10, 9, 97, 116, 32, 111, 114, 103, 46, 115, 112, 114, 105, 110, 103, 102, 114, 97, 109, 101, 119, 111, 114, 107, 46, 107, 97, 102, 107, 97, 46, 108, 105, 115, 116, 101, 110, 101, 114, 46, 75, 97, 102, 107, 97, 77, 101, 115, 115, 97, 103, 101, 76, 105, 115, 116, 101, 110, 101, 114, 67, 111, 110, 116, 97, 105, 110, 101, 114, 36, 76, 105, 115, 116, 101, 110, 101, 114, 67, 111, 110, 115, 117, 109, 101, 114, 46, 100, 111, 73, 110, 118, 111, 107, 101, 87, 105, 116, 104, 82, 101, 99, 111, 114, 100, 115, 40, 75, 97, 102, 107, 97, 77, 101, 115, 115, 97, 103, 101, 76, 105, 115, 116, 101, 110, 101, 114, 67, 111, 110, 116, 97, 105, 110, 101, 114, 46, 106, 97, 118, 97, 58, 49, 53, 52, 57, 41, 10, 9, 97, 116, 32, 111, 114, 103, 46, 115, 112, 114, 105, 110, 103, 102, 114, 97, 109, 101, 119, 111, 114, 107, 46, 107, 97, 102, 107, 97, 46, 108, 105, 115, 116, 101, 110, 101, 114, 46, 75, 97, 102, 107, 97, 77, 101, 115, 115, 97, 103, 101, 76, 105, 115, 116, 101, 110, 101, 114, 67, 111, 110, 116, 97, 105, 110, 101, 114, 36, 76, 105, 115, 116, 101, 110, 101, 114, 67, 111, 110, 115, 117, 109, 101, 114, 46, 105, 110, 118, 111, 107, 101, 82, 101, 99, 111, 114, 100, 76, 105, 115, 116, 101, 110, 101, 114, 40, 75, 97, 102, 107, 97, 77, 101, 115, 115, 97, 103, 101, 76, 105, 115, 116, 101, 110, 101, 114, 67, 111, 110, 116, 97, 105, 110, 101, 114, 46, 106, 97, 118, 97, 58, 49, 52, 53, 55, 41, 10, 9, 97, 116, 32, 111, 114, 103, 46, 115, 112, 114, 105, 110, 103, 102, 114, 97, 109, 101, 119, 111, 114, 107, 46, 107, 97, 102, 107, 97, 46, 108, 105, 115, 116, 101, 110, 101, 114, 46, 75, 97, 102, 107, 97, 77, 101, 115, 115, 97, 103, 101, 76, 105, 115, 116, 101, 110, 101, 114, 67, 111, 110, 116, 97, 105, 110, 101, 114, 36, 76, 105, 115, 116, 101, 110, 101, 114, 67, 111, 110, 115, 117, 109, 101, 114, 46, 105, 110, 118, 111, 107, 101, 76, 105, 115, 116, 101, 110, 101, 114, 40, 75, 97, 102, 107, 97, 77, 101, 115, 115, 97, 103, 101, 76, 105, 115, 116, 101, 110, 101, 114, 67, 111, 110, 116, 97, 105, 110, 101, 114, 46, 106, 97, 118, 97, 58, 49, 50, 48, 55, 41, 10, 9, 97, 116, 32, 111, 114, 103, 46, 115, 112, 114, 105, 110, 103, 102, 114, 97, 109, 101, 119, 111, 114, 107, 46, 107, 97, 102, 107, 97, 46, 108, 105, 115, 116, 101, 110, 101, 114, 46, 75, 97, 102, 107, 97, 77, 101, 115, 115, 97, 103, 101, 76, 105, 115, 116, 101, 110, 101, 114, 67, 111, 110, 116, 97, 105, 110, 101, 114, 36, 76, 105, 115, 116, 101, 110, 101, 114, 67, 111, 110, 115, 117, 109, 101, 114, 46, 112, 111, 108, 108, 65, 110, 100, 73, 110, 118, 111, 107, 101, 40, 75, 97, 102, 107, 97, 77, 101, 115, 115, 97, 103, 101, 76, 105, 115, 116, 101, 110, 101, 114, 67, 111, 110, 116, 97, 105, 110, 101, 114, 46, 106, 97, 118, 97, 58, 57, 54, 54, 41, 10, 9, 97, 116, 32, 111, 114, 103, 46, 115, 112, 114, 105, 110, 103, 102, 114, 97, 109, 101, 119, 111, 114, 107, 46, 107, 97, 102, 107, 97, 46, 108, 105, 115, 116, 101, 110, 101, 114, 46, 75, 97, 102, 107, 97, 77, 101, 115, 115, 97, 103, 101, 76, 105, 115, 116, 101, 110, 101, 114, 67, 111, 110, 116, 97, 105, 110, 101, 114, 36, 76, 105, 115, 116, 101, 110, 101, 114, 67, 111, 110, 115, 117, 109, 101, 114, 46, 114, 117, 110, 40, 75, 97, 102, 107, 97, 77, 101, 115, 115, 97, 103, 101, 76, 105, 115, 116, 101, 110, 101, 114, 67, 111, 110, 116, 97, 105, 110, 101, 114, 46, 106, 97, 118, 97, 58, 57, 48, 49, 41, 10, 9, 97, 116, 32, 106, 97, 118, 97, 46, 117, 116, 105, 108, 46, 99, 111, 110, 99, 117, 114, 114, 101, 110, 116, 46, 69, 120, 101, 99, 117, 116, 111, 114, 115, 36, 82, 117, 110, 110, 97, 98, 108, 101, 65, 100, 97, 112, 116, 101, 114, 46, 99, 97, 108, 108, 40, 69, 120, 101, 99, 117, 116, 111, 114, 115, 46, 106, 97, 118, 97, 58, 53, 49, 49, 41, 10, 9, 97, 116, 32, 106, 97, 118, 97, 46, 117, 116, 105, 108, 46, 99, 111, 110, 99, 117, 114, 114, 101, 110, 116, 46, 70, 117, 116, 117, 114, 101, 84, 97, 115, 107, 46, 114, 117, 110, 40, 70, 117, 116, 117, 114, 101, 84, 97, 115, 107, 46, 106, 97, 118, 97, 58, 50, 54, 54, 41, 10, 9, 97, 116, 32, 106, 97, 118, 97, 46, 108, 97, 110, 103, 46, 84, 104, 114, 101, 97, 100, 46, 114, 117, 110, 40, 84, 104, 114, 101, 97, 100, 46, 106, 97, 118, 97, 58, 55, 52, 56, 41, 10, 67, 97, 117, 115, 101, 100, 32, 98, 121, 58, 32, 106, 97, 118, 97, 46, 108, 97, 110, 103, 46, 82, 117, 110, 116, 105, 109, 101, 69, 120, 99, 101, 112, 116, 105, 111, 110, 58, 32, 102, 97, 105, 108, 101, 100, 10, 9, 97, 116, 32, 111, 114, 103, 46, 120, 109, 46, 108, 112, 46, 101, 118, 101, 110, 116, 46, 97, 112, 105, 46, 108, 105, 115, 116, 101, 110, 101, 114, 46, 69, 118, 101, 110, 116, 76, 105, 115, 116, 101, 110, 101, 114, 46, 112, 114, 111, 99, 101, 115, 115, 69, 118, 101, 110, 116, 40, 69, 118, 101, 110, 116, 76, 105, 115, 116, 101, 110, 101, 114, 46, 106, 97, 118, 97, 58, 51, 53, 41, 10, 9, 97, 116, 32, 115, 117, 110, 46, 114, 101, 102, 108, 101, 99, 116, 46, 78, 97, 116, 105, 118, 101, 77, 101, 116, 104, 111, 100, 65, 99, 99, 101, 115, 115, 111, 114, 73, 109, 112, 108, 46, 105, 110, 118, 111, 107, 101, 48, 40, 78, 97, 116, 105, 118, 101, 32, 77, 101, 116, 104, 111, 100, 41, 10, 9, 97, 116, 32, 115, 117, 110, 46, 114, 101, 102, 108, 101, 99, 116, 46, 78, 97, 116, 105, 118, 101, 77, 101, 116, 104, 111, 100, 65, 99, 99, 101, 115, 115, 111, 114, 73, 109, 112, 108, 46, 105, 110, 118, 111, 107, 101, 40, 78, 97, 116, 105, 118, 101, 77, 101, 116, 104, 111, 100, 65, 99, 99, 101, 115, 115, 111, 114, 73, 109, 112, 108, 46, 106, 97, 118, 97, 58, 54, 50, 41, 10, 9, 97, 116, 32, 115, 117, 110, 46, 114, 101, 102, 108, 101, 99, 116, 46, 68, 101, 108, 101, 103, 97, 116, 105, 110, 103, 77, 101, 116, 104, 111, 100, 65, 99, 99, 101, 115, 115, 111, 114, 73, 109, 112, 108, 46, 105, 110, 118, 111, 107, 101, 40, 68, 101, 108, 101, 103, 97, 116, 105, 110, 103, 77, 101, 116, 104, 111, 100, 65, 99, 99, 101, 115, 115, 111, 114, 73, 109, 112, 108, 46, 106, 97, 118, 97, 58, 52, 51, 41, 10, 9, 97, 116, 32, 106, 97, 118, 97, 46, 108, 97, 110, 103, 46, 114, 101, 102, 108, 101, 99, 116, 46, 77, 101, 116, 104, 111, 100, 46, 105, 110, 118, 111, 107, 101, 40, 77, 101, 116, 104, 111, 100, 46, 106, 97, 118, 97, 58, 52, 57, 56, 41, 10, 9, 97, 116, 32, 111, 114, 103, 46, 115, 112, 114, 105, 110, 103, 102, 114, 97, 109, 101, 119, 111, 114, 107, 46, 109, 101, 115, 115, 97, 103, 105, 110, 103, 46, 104, 97, 110, 100, 108, 101, 114, 46, 105, 110, 118, 111, 99, 97, 116, 105, 111, 110, 46, 73, 110, 118, 111, 99, 97, 98, 108, 101, 72, 97, 110, 100, 108, 101, 114, 77, 101, 116, 104, 111, 100, 46, 100, 111, 73, 110, 118, 111, 107, 101, 40, 73, 110, 118, 111, 99, 97, 98, 108, 101, 72, 97, 110, 100, 108, 101, 114, 77, 101, 116, 104, 111, 100, 46, 106, 97, 118, 97, 58, 49, 55, 49, 41, 10, 9, 97, 116, 32, 111, 114, 103, 46, 115, 112, 114, 105, 110, 103, 102, 114, 97, 109, 101, 119, 111, 114, 107, 46, 109, 101, 115, 115, 97, 103, 105, 110, 103, 46, 104, 97, 110, 100, 108, 101, 114, 46, 105, 110, 118, 111, 99, 97, 116, 105, 111, 110, 46, 73, 110, 118, 111, 99, 97, 98, 108, 101, 72, 97, 110, 100, 108, 101, 114, 77, 101, 116, 104, 111, 100, 46, 105, 110, 118, 111, 107, 101, 40, 73, 110, 118, 111, 99, 97, 98, 108, 101, 72, 97, 110, 100, 108, 101, 114, 77, 101, 116, 104, 111, 100, 46, 106, 97, 118, 97, 58, 49, 50, 48, 41, 10, 9, 97, 116, 32, 111, 114, 103, 46, 115, 112, 114, 105, 110, 103, 102, 114, 97, 109, 101, 119, 111, 114, 107, 46, 107, 97, 102, 107, 97, 46, 108, 105, 115, 116, 101, 110, 101, 114, 46, 97, 100, 97, 112, 116, 101, 114, 46, 72, 97, 110, 100, 108, 101, 114, 65, 100, 97, 112, 116, 101, 114, 46, 105, 110, 118, 111, 107, 101, 40, 72, 97, 110, 100, 108, 101, 114, 65, 100, 97, 112, 116, 101, 114, 46, 106, 97, 118, 97, 58, 52, 56, 41, 10, 9, 97, 116, 32, 111, 114, 103, 46, 115, 112, 114, 105, 110, 103, 102, 114, 97, 109, 101, 119, 111, 114, 107, 46, 107, 97, 102, 107, 97, 46, 108, 105, 115, 116, 101, 110, 101, 114, 46, 97, 100, 97, 112, 116, 101, 114, 46, 77, 101, 115, 115, 97, 103, 105, 110, 103, 77, 101, 115, 115, 97, 103, 101, 76, 105, 115, 116, 101, 110, 101, 114, 65, 100, 97, 112, 116, 101, 114, 46, 105, 110, 118, 111, 107, 101, 72, 97, 110, 100, 108, 101, 114, 40, 77, 101, 115, 115, 97, 103, 105, 110, 103, 77, 101, 115, 115, 97, 103, 101, 76, 105, 115, 116, 101, 110, 101, 114, 65, 100, 97, 112, 116, 101, 114, 46, 106, 97, 118, 97, 58, 51, 48, 52, 41, 10, 9, 97, 116, 32, 111, 114, 103, 46, 115, 112, 114, 105, 110, 103, 102, 114, 97, 109, 101, 119, 111, 114, 107, 46, 107, 97, 102, 107, 97, 46, 108, 105, 115, 116, 101, 110, 101, 114, 46, 97, 100, 97, 112, 116, 101, 114, 46, 82, 101, 99, 111, 114, 100, 77, 101, 115, 115, 97, 103, 105, 110, 103, 77, 101, 115, 115, 97, 103, 101, 76, 105, 115, 116, 101, 110, 101, 114, 65, 100, 97, 112, 116, 101, 114, 46, 111, 110, 77, 101, 115, 115, 97, 103, 101, 40, 82, 101, 99, 111, 114, 100, 77, 101, 115, 115, 97, 103, 105, 110, 103, 77, 101, 115, 115, 97, 103, 101, 76, 105, 115, 116, 101, 110, 101, 114, 65, 100, 97, 112, 116, 101, 114, 46, 106, 97, 118, 97, 58, 56, 54, 41, 10, 9, 97, 116, 32, 111, 114, 103, 46, 115, 112, 114, 105, 110, 103, 102, 114, 97, 109, 101, 119, 111, 114, 107, 46, 107, 97, 102, 107, 97, 46, 108, 105, 115, 116, 101, 110, 101, 114, 46, 97, 100, 97, 112, 116, 101, 114, 46, 82, 101, 99, 111, 114, 100, 77, 101, 115, 115, 97, 103, 105, 110, 103, 77, 101, 115, 115, 97, 103, 101, 76, 105, 115, 116, 101, 110, 101, 114, 65, 100, 97, 112, 116, 101, 114, 46, 111, 110, 77, 101, 115, 115, 97, 103, 101, 40, 82, 101, 99, 111, 114, 100, 77, 101, 115, 115, 97, 103, 105, 110, 103, 77, 101, 115, 115, 97, 103, 101, 76, 105, 115, 116, 101, 110, 101, 114, 65, 100, 97, 112, 116, 101, 114, 46, 106, 97, 118, 97, 58, 53, 49, 41, 10, 9, 97, 116, 32, 111, 114, 103, 46, 115, 112, 114, 105, 110, 103, 102, 114, 97, 109, 101, 119, 111, 114, 107, 46, 107, 97, 102, 107, 97, 46, 108, 105, 115, 116, 101, 110, 101, 114, 46, 97, 100, 97, 112, 116, 101, 114, 46, 82, 101, 116, 114, 121, 105, 110, 103, 77, 101, 115, 115, 97, 103, 101, 76, 105, 115, 116, 101, 110, 101, 114, 65, 100, 97, 112, 116, 101, 114, 46, 108, 97, 109, 98, 100, 97, 36, 111, 110, 77, 101, 115, 115, 97, 103, 101, 36, 48, 40, 82, 101, 116, 114, 121, 105, 110, 103, 77, 101, 115, 115, 97, 103, 101, 76, 105, 115, 116, 101, 110, 101, 114, 65, 100, 97, 112, 116, 101, 114, 46, 106, 97, 118, 97, 58, 49, 50, 48, 41, 10, 9, 97, 116, 32, 111, 114, 103, 46, 115, 112, 114, 105, 110, 103, 102, 114, 97, 109, 101, 119, 111, 114, 107, 46, 114, 101, 116, 114, 121, 46, 115, 117, 112, 112, 111, 114, 116, 46, 82, 101, 116, 114, 121, 84, 101, 109, 112, 108, 97, 116, 101, 46, 100, 111, 69, 120, 101, 99, 117, 116, 101, 40, 82, 101, 116, 114, 121, 84, 101, 109, 112, 108, 97, 116, 101, 46, 106, 97, 118, 97, 58, 50, 56, 55, 41, 10, 9, 97, 116, 32, 111, 114, 103, 46, 115, 112, 114, 105, 110, 103, 102, 114, 97, 109, 101, 119, 111, 114, 107, 46, 114, 101, 116, 114, 121, 46, 115, 117, 112, 112, 111, 114, 116, 46, 82, 101, 116, 114, 121, 84, 101, 109, 112, 108, 97, 116, 101, 46, 101, 120, 101, 99, 117, 116, 101, 40, 82, 101, 116, 114, 121, 84, 101, 109, 112, 108, 97, 116, 101, 46, 106, 97, 118, 97, 58, 50, 49, 49, 41, 10, 9, 97, 116, 32, 111, 114, 103, 46, 115, 112, 114, 105, 110, 103, 102, 114, 97, 109, 101, 119, 111, 114, 107, 46, 107, 97, 102, 107, 97, 46, 108, 105, 115, 116, 101, 110, 101, 114, 46, 97, 100, 97, 112, 116, 101, 114, 46, 82, 101, 116, 114, 121, 105, 110, 103, 77, 101, 115, 115, 97, 103, 101, 76, 105, 115, 116, 101, 110, 101, 114, 65, 100, 97, 112, 116, 101, 114, 46, 111, 110, 77, 101, 115, 115, 97, 103, 101, 40, 82, 101, 116, 114, 121, 105, 110, 103, 77, 101, 115, 115, 97, 103, 101, 76, 105, 115, 116, 101, 110, 101, 114, 65, 100, 97, 112, 116, 101, 114, 46, 106, 97, 118, 97, 58, 49, 49, 52, 41, 10, 9, 97, 116, 32, 111, 114, 103, 46, 115, 112, 114, 105, 110, 103, 102, 114, 97, 109, 101, 119, 111, 114, 107, 46, 107, 97, 102, 107, 97, 46, 108, 105, 115, 116, 101, 110, 101, 114, 46, 97, 100, 97, 112, 116, 101, 114, 46, 82, 101, 116, 114, 121, 105, 110, 103, 77, 101, 115, 115, 97, 103, 101, 76, 105, 115, 116, 101, 110, 101, 114, 65, 100, 97, 112, 116, 101, 114, 46, 111, 110, 77, 101, 115, 115, 97, 103, 101, 40, 82, 101, 116, 114, 121, 105, 110, 103, 77, 101, 115, 115, 97, 103, 101, 76, 105, 115, 116, 101, 110, 101, 114, 65, 100, 97, 112, 116, 101, 114, 46, 106, 97, 118, 97, 58, 52, 48, 41, 10, 9, 97, 116, 32, 111, 114, 103, 46, 115, 112, 114, 105, 110, 103, 102, 114, 97, 109, 101, 119, 111, 114, 107, 46, 107, 97, 102, 107, 97, 46, 108, 105, 115, 116, 101, 110, 101, 114, 46, 75, 97, 102, 107, 97, 77, 101, 115, 115, 97, 103, 101, 76, 105, 115, 116, 101, 110, 101, 114, 67, 111, 110, 116, 97, 105, 110, 101, 114, 36, 76, 105, 115, 116, 101, 110, 101, 114, 67, 111, 110, 115, 117, 109, 101, 114, 46, 100, 111, 73, 110, 118, 111, 107, 101, 79, 110, 77, 101, 115, 115, 97, 103, 101, 40, 75, 97, 102, 107, 97, 77, 101, 115, 115, 97, 103, 101, 76, 105, 115, 116, 101, 110, 101, 114, 67, 111, 110, 116, 97, 105, 110, 101, 114, 46, 106, 97, 118, 97, 58, 49, 54, 55, 48, 41, 10, 9, 97, 116, 32, 111, 114, 103, 46, 115, 112, 114, 105, 110, 103, 102, 114, 97, 109, 101, 119, 111, 114, 107, 46, 107, 97, 102, 107, 97, 46, 108, 105, 115, 116, 101, 110, 101, 114, 46, 75, 97, 102, 107, 97, 77, 101, 115, 115, 97, 103, 101, 76, 105, 115, 116, 101, 110, 101, 114, 67, 111, 110, 116, 97, 105, 110, 101, 114, 36, 76, 105, 115, 116, 101, 110, 101, 114, 67, 111, 110, 115, 117, 109, 101, 114, 46, 105, 110, 118, 111, 107, 101, 79, 110, 77, 101, 115, 115, 97, 103, 101, 40, 75, 97, 102, 107, 97, 77, 101, 115, 115, 97, 103, 101, 76, 105, 115, 116, 101, 110, 101, 114, 67, 111, 110, 116, 97, 105, 110, 101, 114, 46, 106, 97, 118, 97, 58, 49, 54, 53, 51, 41, 10, 9, 97, 116, 32, 111, 114, 103, 46, 115, 112, 114, 105, 110, 103, 102, 114, 97, 109, 101, 119, 111, 114, 107, 46, 107, 97, 102, 107, 97, 46, 108, 105, 115, 116, 101, 110, 101, 114, 46, 75, 97, 102, 107, 97, 77, 101, 115, 115, 97, 103, 101, 76, 105, 115, 116, 101, 110, 101, 114, 67, 111, 110, 116, 97, 105, 110, 101, 114, 36, 76, 105, 115, 116, 101, 110, 101, 114, 67, 111, 110, 115, 117, 109, 101, 114, 46, 100, 111, 73, 110, 118, 111, 107, 101, 82, 101, 99, 111, 114, 100, 76, 105, 115, 116, 101, 110, 101, 114, 40, 75, 97, 102, 107, 97, 77, 101, 115, 115, 97, 103, 101, 76, 105, 115, 116, 101, 110, 101, 114, 67, 111, 110, 116, 97, 105, 110, 101, 114, 46, 106, 97, 118, 97, 58, 49, 54, 48, 56, 41, 10, 9, 46, 46, 46, 32, 56, 32, 109, 111, 114, 101, 10])], isReadOnly = true), key=null, value=Hello(name=Ankit, Payload=[{"serialNo": "3545145628662", "eventTime": "2020-04-22 23:59:59"}]), timestamp=null)
java.lang.IllegalArgumentException: Magic v1 does not support record headers
at org.apache.kafka.common.record.MemoryRecordsBuilder.appendWithOffset(MemoryRecordsBuilder.java:412) ~[kafka-clients-2.3.1.jar:na]
at org.apache.kafka.common.record.MemoryRecordsBuilder.appendWithOffset(MemoryRecordsBuilder.java:451) ~[kafka-clients-2.3.1.jar:na]
at org.apache.kafka.common.record.MemoryRecordsBuilder.append(MemoryRecordsBuilder.java:508) ~[kafka-clients-2.3.1.jar:na]
at org.apache.kafka.common.record.MemoryRecordsBuilder.append(MemoryRecordsBuilder.java:531) ~[kafka-clients-2.3.1.jar:na]
at org.apache.kafka.clients.producer.internals.ProducerBatch.tryAppend(ProducerBatch.java:106) ~[kafka-clients-2.3.1.jar:na]
at org.apache.kafka.clients.producer.internals.RecordAccumulator.append(RecordAccumulator.java:224) ~[kafka-clients-2.3.1.jar:na]
at org.apache.kafka.clients.producer.KafkaProducer.doSend(KafkaProducer.java:917) ~[kafka-clients-2.3.1.jar:na]
at org.apache.kafka.clients.producer.KafkaProducer.send(KafkaProducer.java:856) ~[kafka-clients-2.3.1.jar:na]
at org.springframework.kafka.core.DefaultKafkaProducerFactory$CloseSafeProducer.send(DefaultKafkaProducerFactory.java:590) ~[spring-kafka-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.kafka.core.KafkaTemplate.doSend(KafkaTemplate.java:404) ~[spring-kafka-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.kafka.core.KafkaTemplate.send(KafkaTemplate.java:241) ~[spring-kafka-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.kafka.listener.DeadLetterPublishingRecoverer.publish(DeadLetterPublishingRecoverer.java:214) [spring-kafka-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.kafka.listener.DeadLetterPublishingRecoverer.accept(DeadLetterPublishingRecoverer.java:159) [spring-kafka-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.kafka.listener.DeadLetterPublishingRecoverer.accept(DeadLetterPublishingRecoverer.java:53) [spring-kafka-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.kafka.listener.FailedRecordTracker.skip(FailedRecordTracker.java:101) [spring-kafka-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.kafka.support.SeekUtils.lambda$doSeeks$2(SeekUtils.java:75) ~[spring-kafka-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at java.util.ArrayList.forEach(ArrayList.java:1257) ~[na:1.8.0_242]
at org.springframework.kafka.support.SeekUtils.doSeeks(SeekUtils.java:72) ~[spring-kafka-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.kafka.listener.SeekToCurrentErrorHandler.handle(SeekToCurrentErrorHandler.java:198) ~[spring-kafka-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.invokeErrorHandler(KafkaMessageListenerContainer.java:1704) ~[spring-kafka-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.doInvokeRecordListener(KafkaMessageListenerContainer.java:1621) ~[spring-kafka-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.doInvokeWithRecords(KafkaMessageListenerContainer.java:1549) ~[spring-kafka-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.invokeRecordListener(KafkaMessageListenerContainer.java:1457) ~[spring-kafka-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.invokeListener(KafkaMessageListenerContainer.java:1207) ~[spring-kafka-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.pollAndInvoke(KafkaMessageListenerContainer.java:966) ~[spring-kafka-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at org.springframework.kafka.listener.KafkaMessageListenerContainer$ListenerConsumer.run(KafkaMessageListenerContainer.java:901) ~[spring-kafka-2.3.4.RELEASE.jar:2.3.4.RELEASE]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) ~[na:1.8.0_242]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) ~[na:1.8.0_242]
at java.lang.Thread.run(Thread.java:748) ~[na:1.8.0_242]
The error says:
Magic v1 does not support record headers
record headers are introduced in Apache Kafka 0.11.0.0. Can it be that your brokers are older or that the topic you are using is using an older message format (note, that if a topic is created with an old broker and the brokers are updated, the older message format is kept if not explicitly updated, too).

ruby incompatible encodings when moving files

I'm (unfortunately) trying to move/rename a file with UTF-8 chars in it's name. For some reason when I try to move it, I get this error:
ERROR Encoding::CompatibilityError: incompatible character encodings: ASCII-8BIT and UTF-8
I've added begin..rescue block to see the exception thrown, and I get that no file of such name exists (it does exists, I've checked). Currently the strings are encoded in UTF-8. I'm running windows, and I'm not using Rails. Here are the bytes of the filename (E:/Filmy/Avengers 2 - Věk Ultrona (2015) cz dabing, akční, scifi BRRip.avi):
[69, 58, 47, 70, 105, 108, 109, 121, 47, 65, 118, 101, 110, 103, 101, 114, 115, 32, 50, 32, 45, 32, 86, 195, 132, 226, 128, 186, 107, 32, 85, 108, 116, 114, 111, 110, 97, 32, 40, 50, 48, 49, 53, 41, 32, 99, 122, 32, 100, 97, 98, 105, 110, 103, 44, 32, 97, 107, 195, 132, 197, 164, 110, 196, 130, 194, 173, 44, 32, 115, 99, 105, 102, 105, 32, 66, 82, 82, 105, 112, 46, 97, 118, 105]
To move the file I'm just using the simple:
File.rename(from, to)
What encoding should I use? Any other ideas?
Thank you

Ruby 2.3.1 sort_by function change?

Here's a small ruby script:
p "ruby #{ RUBY_VERSION }p#{ RUBY_PATCHLEVEL }"
p 100.times.collect{|i| i}.sort_by{|j| j % 1}
I would have expect the same result from a version to another. In my case, it's not. Here's the results
"ruby 2.2.3p173"
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99
"ruby 2.3.1p112"
[99, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 0]
Is that normal?
Ruby doesn't guarantee you a sort order if the items are the same.
As to why the result changed between versions, this looks like a relevant change: ruby 2.3 tries to use a c-standard library provided implementation of quicksort in more cases than before.
Have a look at Is sort in Ruby stable?. The quick answer is no, it's not. What this means is that if two values are equivalent, in your case always equal to 0, you can't make any assumptions about where they go in relation to one another.

Sort ActiveRecord query response by Globalize3 translation on an association using locale

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

Resources