Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sql-server-2005/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何使用RubyonRails5.2编写复杂的连接查询?_Ruby On Rails_Activerecord - Fatal编程技术网

Ruby on rails 如何使用RubyonRails5.2编写复杂的连接查询?

Ruby on rails 如何使用RubyonRails5.2编写复杂的连接查询?,ruby-on-rails,activerecord,Ruby On Rails,Activerecord,我的应用程序以多种语言管理业务规则描述。为了显示业务规则的索引(名称和描述),我需要将BusinessRule模型加入到翻译模型中。翻译模型是多态的: Table name: translations id :integer not null, primary key document_type :string document_id :bigint(8) field_name :string(30) not nu

我的应用程序以多种语言管理业务规则描述。为了显示业务规则的索引(名称和描述),我需要将BusinessRule模型加入到翻译模型中。翻译模型是多态的:

 Table name: translations

  id            :integer          not null, primary key
  document_type :string
  document_id   :bigint(8)
  field_name    :string(30)       not null
  language      :string(2)        not null
  translation   :text
  searchable    :tsvector
  created_at    :datetime         not null
  updated_at    :datetime         not null
为了简化,我在business_rule.rb模型中定义了与业务规则的关系别名:

  has_many :name_translations, -> { where("field_name='name'") }, class_name: 'Translation', as: :document
  has_many :description_translations, -> { where("field_name='description'") }, class_name: 'Translation', as: :document
要构建索引,我需要将业务规则分别与当前用户语言的名称翻译和描述翻译(在SQL中变为):

select BR.code, TR1.translation as name, TR2.translation as description
from dqm_app.business_rules BR
left outer join dqm_app.translations TR1 on (TR1.document_id = BR.id and TR1.field_name='name' and TR1.language='en')
left outer join dqm_app.translations TR2 on (TR2.document_id = BR.id and TR2.field_name='description' and TR2.language='en')
我尝试将其转换为(假设用户的语言为英语):

这引发了两个问题:

1-ActiveRecord::StatementInvalid(PG::AmbiguousColumn:错误:column 引用“field_name”不明确[当然,它在查询中出现两次]

2.where条件不正确,我不知道如何构建它

你能帮我吗

非常感谢

要解决(1)将作用域更改为使用散列语法,Rails将添加消歧,例如

has_many :name_translations, -> { where(field_name: 'name') }, class_name: 'Translation', as: :document
要解决(2)问题,取决于
BusinessRule
中是否有存储语言的字段?如果有,那么您就知道Rails将在您允许的情况下将
BusinessRule
实例本身传递到作用域,因此您可以执行如下操作:

has_many :name_translations, -> br { where(field_name: 'name', language: br.language) }, class_name: 'Translation', as: :document

多亏了斯马蒂,我终于解决了两个问题:

1-在BusinessRule模型中添加了哈希语法消歧

has_many :name_translations, -> { where(field_name: 'name') }, class_name: 'Translation', as: :document
has_many :description_translations, -> { where(field_name: 'description') }, class_name: 'Translation', as: :document
2-由于BusinessRule模型不存储语言,但用户配置文件存储语言,因此在索引方法中,我添加了一个where子句,该子句引用翻译表:


请注意,我没有设法引用where子句:name\u translations或:description\u translations。

如果您使用Arel,像这样的复杂查询可以分解为更小的逻辑、命名和组合,就像您使用其他方法一样。
has_many :name_translations, -> { where(field_name: 'name') }, class_name: 'Translation', as: :document
has_many :description_translations, -> { where(field_name: 'description') }, class_name: 'Translation', as: :document
  @business_rules = BusinessRule.left_outer_joins(:name_translations, :description_translations).
    where(translations: {language: user_language})