crbelaus / trans

Embedded translations for Ecto
https://hex.pm/packages/trans
Apache License 2.0
231 stars 18 forks source link

Issues when trying to order by a translated field #45

Closed speeddragon closed 7 years ago

speeddragon commented 7 years ago

I'm trying to order a query result by a specific field in a language, but I don't see any examples and my attempt to doing doesn't work.

I'm trying something like this: order_by(asc: translated(FoodItem, :name, ^language))

And the error on compile is the following,

== Compilation error on file lib/ketchup/food_item/food_item_helper.ex ==
** (ArgumentError) The locale code must be either an atom or a string
    lib/trans/query_builder.ex:104: Trans.QueryBuilder.locale/1
    expanding macro: Trans.QueryBuilder.translated/3

language is always an atom, but I don't understand how is checking in compile time instead in runtime.

I managed to get this work by using fragment,

|> order_by(asc: fragment("translations -> ? -> 'name'", ^Atom.to_string(language)))

crbelaus commented 7 years ago

Thank you for the report @speeddragon 🙌. I will take a look into this bug.

crbelaus commented 7 years ago

This should be now fixed with the 2.0.2 release.

crbelaus commented 7 years ago

I am closing this issue since it has been fixed two weeks ago. Feel free to open a new one if you encounter any more problems!