【问题标题】:Rails and Globalize - join translation table of related model in scopeRails 和 Globalize - 加入范围内相关模型的翻译表
【发布时间】:2015-04-10 08:51:03
【问题描述】:

我想订购与另一个模型有关系的模型的一些记录(带有翻译的属性)。举个例子:

  1. 我有一个模型项目
  2. 我有一个模型任务
  3. 我有关系 Project has_many Tasks
  4. 模型 Task 的属性 name 已全球化(在 task_translations 表上)

现在,我想按任务名称对所有项目进行排序。我怎样才能写这个范围?如何加入 Rails 中的翻译表,如 gem globalize (https://github.com/globalize/globalize/blob/eccb924ac9641b52399f22525b0e3ec004739f4c/lib/globalize/active_record/class_methods.rb) 中的方法 with_translation 但从相关对象 Project

> Project.all.joins(:tasks) ... (how to include task translation table) ...

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 relationship has-many-through globalize


    【解决方案1】:

    我认为task_translationstasks 直接相关,您可以这样查询:

    Project.joins(tasks: :translations)
    

    【讨论】:

    • 这就是您访问表格的方式Project.joins(tasks: :translations).where("task_translations.name = ?", "todo") }
    • @Epigene 不同的语言环境怎么样?
    • @TalhaShoaib 假设语言环境是task_translations 表中的一列,那么额外的.where("task_translations.locale = ?", locale) 应该这样做。
    猜你喜欢
    • 2013-09-16
    • 1970-01-01
    • 2015-04-20
    • 2015-11-15
    • 1970-01-01
    • 2012-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多