Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails and Globalize - join translation table of related model in scope

I want to order some records of a model that has a relation to another model (with translated attributes). Here an example:

  1. I have a model Project
  2. I have a model Task
  3. I have the relation Project has_many Tasks
  4. The model Task has attribute name globalized (on task_translations table)

Now, I want to order all projects by its tasks name. How can I write this scope? How can I join the translation table in Rails like method with_translation in gem globalize (https://github.com/globalize/globalize/blob/eccb924ac9641b52399f22525b0e3ec004739f4c/lib/globalize/active_record/class_methods.rb) but from related object Project?

> Project.all.joins(:tasks) ... (how to include task translation table) ...
like image 719
phlegx Avatar asked Feb 10 '15 10:02

phlegx


1 Answers

I believe the task_translations is directly related to tasks and you can query it like so:

Project.joins(tasks: :translations)
like image 100
gabrielhilal Avatar answered Nov 15 '22 04:11

gabrielhilal