ruby-on-rails - 无法使用 Mobility 按 Rails 中翻译的列进行排序

标签 ruby-on-rails mobility

这个问题基于 issue发布到 Mobility GitHub 项目。

上下文

  • Rails:5.0.6
  • 移动性:0.4.2(带表格后端)

我正在使用支持多种文章类型(例如博客文章、案例研究、知识库文章)的文章表。该表包含一个用于跟踪文章被查看次数的列,这是一个整数列,每次 show 时都会递增。文章需要采取行动。

在实现这些文章的翻译时,我想单独跟踪每个翻译的浏览次数,而不是主要文章对象的浏览次数。为了实现这一目标,我添加了 views属性作为我的对象的翻译属性之一:

class Article < ApplicationRecord
  include Taggable
  include PgSearch

  translates :title, type: :string
  translates :subtitle, type: :text
  translates :body, type: :text
  translates :views, type: :integer

  multisearchable :against => [:title, :subtitle, :body]

  has_and_belongs_to_many :products

  attachment :hero_image, content_type: %w(image/jpeg image/png image/gif)

  validates :title, :body, :posted_on, presence: true

  scope :current, -> { where 'posted_on < ?', Date.tomorrow }
  scope :news_articles, -> { where type: ['BlogPost', 'CaseStudy'] }

  def log_view(by = 1)
    self.views ||= 0
    self.views += by
    self.save(touch: false)
  end

  def to_param
    "#{id} #{title}".parameterize
  end

  def published?
    posted_on < Date.tomorrow
  end
end

预期行为

在我的 Controller 中,我想列出通过以下查询获得的浏览量最高的十篇文章:

@top_articles = Article.current.news_articles.order(views: :desc, posted_on: :desc).limit(10)

我希望收到一系列文章,就像我在实现 Mobility 之前所做的那样。

实际行为

我得到的是 #<Article::ActiveRecord_Relation:0x233686c> 。如果我然后尝试将其转换为带有 @top_articles.to_a 的数组,我收到此错误:

  Article Load (0.7ms)  SELECT  "articles".* FROM "articles" WHERE (posted_on < '2018-02-11') AND "articles"."type" IN ('BlogPost', 'CaseStudy') ORDER BY "articles"."views" DESC, "articles"."posted_on" DESC LIMIT $1  [["LIMIT", 10]]
ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR:  column articles.views does not exist
LINE 1: ...les"."type" IN ('BlogPost', 'CaseStudy') ORDER BY "articles"...
                                                             ^
: SELECT  "articles".* FROM "articles" WHERE (posted_on < '2018-02-11') AND "articles"."type" IN ('BlogPost', 'CaseStudy') ORDER BY "articles"."views" DESC, "articles"."posted_on" DESC LIMIT $1
from /usr/local/bundle/gems/activerecord-5.0.6/lib/active_record/connection_adapters/postgresql_adapter.rb:598:in `async_exec'

更改查询以包含 i18n :

@top_articles = Article.i18n.current.news_articles.order(views: :desc, posted_on: :desc).limit(10)

...返回#<#<Mobility::Backends::ActiveRecord::Table::QueryMethods:0x00000000050a86d8>:0x286c3e0> ,当我尝试将其转换为数组时,我得到了同样的结果:

  Article Load (0.7ms)  SELECT  "articles".* FROM "articles" WHERE (posted_on < '2018-02-11') AND "articles"."type" IN ('BlogPost', 'CaseStudy') ORDER BY "articles"."views" DESC, "articles"."posted_on" DESC LIMIT $1  [["LIMIT", 10]]
ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR:  column articles.views does not exist
LINE 1: ...les"."type" IN ('BlogPost', 'CaseStudy') ORDER BY "articles"...
                                                             ^
: SELECT  "articles".* FROM "articles" WHERE (posted_on < '2018-02-11') AND "articles"."type" IN ('BlogPost', 'CaseStudy') ORDER BY "articles"."views" DESC, "articles"."posted_on" DESC LIMIT $1
from /usr/local/bundle/gems/activerecord-5.0.6/lib/active_record/connection_adapters/postgresql_adapter.rb:598:in `async_exec'

事实证明,虽然 Mobility 支持使用 where 中的翻译字段条款,目前不支持 order 中的它们Active Record 查询的子句。

解决方法尝试

1。引用order中的翻译表条款

根据 gem 作者的反馈,我尝试了以下查询:

Article.i18n.current.news_articles.order('article_translations.views desc', 'articles.posted_on desc')

...返回 #<#<Mobility::Backends::ActiveRecord::Table::QueryMethods>>对象,以及to_a返回此错误:

ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  missing FROM-clause entry for table "article_translations"
LINE 1: ...les"."type" IN ('BlogPost', 'CaseStudy') ORDER BY article_tr...
                                                             ^
: SELECT  "articles".* FROM "articles" WHERE (posted_on < '2018-02-12') AND "articles"."type" IN ('BlogPost', 'CaseStudy') ORDER BY article_translations.views desc, articles.posted_on desc LIMIT $1
from /usr/local/bundle/gems/activerecord-5.0.6/lib/active_record/connection_adapters/postgresql_adapter.rb:598:in `async_exec'

2。添加joinsincludes翻译表子句

Article.i18n.joins(:article_translations).order('article_translations.views desc', 'articles.posted_on desc').limit(10)

此查询再次返回 #<#<Mobility::Backends::ActiveRecord::Table::QueryMethods>>对象,以及to_a结果:

ActiveRecord::ConfigurationError: Can't join 'Article' to association named 'article_translations'; perhaps you misspelled it?
from /usr/local/bundle/gems/activerecord-5.0.6/lib/active_record/associations/join_dependency.rb:231:in `find_reflection'

3。添加has_many :article_translations模型

向模型添加关系会引发此错误:

uninitialized constant Article::ArticleTranslation

所以...

接下来我应该尝试什么?

最佳答案

更新

从版本 0.8.0 开始支持按翻译属性排序/

只需这样做:

Article.i18n.current.news_articles.
  order(:views => :desc, :'articles.posted_on' => :desc)

Mobility 将处理一切(您不需要加入翻译表等)

原始答案

您是对的,您需要加入翻译表,但关联名称为 translations,而不是 article_translations

无论如何,有一个方法join_translations可以连接翻译表,所以这应该可以工作:

Article.i18n.
  current.
  news_articles.
  join_translations.
  order('article_translations.views desc', 'articles.posted_on desc')

关于ruby-on-rails - 无法使用 Mobility 按 Rails 中翻译的列进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48728763/

相关文章:

MySQL Rails 迁移错误 : "Error on rename of schema_migration (errno: -1)"

ruby-on-rails - 每当 gem cronjob 不从/etc/environment 加载 PATH

ruby-on-rails - 使用 Elastic Search gem 运行 jruby 应用程序。

javascript - 绝对初学者的 Node.js/Express.j 或 Ruby on Rails

javascript - jquery 使用嵌套数组获取 json

android - 设备策略 Controller (DCP) 应用程序和系统应用程序之间有什么区别?

linux - WSO2 企业移动管理器

ruby-on-rails - 移动 gem : Adding translations to an existing model (that already has a lot of records)?

java - Jade Library-容器之间的代理移动性

ios - iOS 中的四位数密码输入/OTP 屏幕