Ruby on rails 当在作用域中使用时,是否有一个关联独立工作,但不返回任何内容

Ruby on rails 当在作用域中使用时,是否有一个关联独立工作,但不返回任何内容,ruby-on-rails,activerecord,scope,associations,Ruby On Rails,Activerecord,Scope,Associations,我知道这不是一个很小的问题,但请耐心一点——我想我错过了什么,但不知道是什么 class Company::ExpositionTarget::DiscretionaryNote < ActiveRecord::Base belongs_to :company_exposition_target, class_name: ::Company::ExpositionTarget end class Company::ExpositionTarget < ActiveRec

我知道这不是一个很小的问题,但请耐心一点——我想我错过了什么,但不知道是什么

class Company::ExpositionTarget::DiscretionaryNote < ActiveRecord::Base
  belongs_to :company_exposition_target,
    class_name: ::Company::ExpositionTarget
end

class Company::ExpositionTarget < ActiveRecord::Base
    has_many :discretionary_notes,
      class_name: ::Company::ExpositionTarget::DiscretionaryNote,
      foreign_key: :company_exposition_target_id

    has_one :last_discretionary_note,
      ->(exposition_target) { where(company_exposition_target: exposition_target).order(created_at: :desc).limit(1) },
      class_name: ::Company::ExpositionTarget::DiscretionaryNote,
      foreign_key: :company_exposition_target_id
end
生成的SQL:

  SELECT  company_exposition_targets.*
  FROM company_exposition_targets
  WHERE company_exposition_targets.id = 102
  LIMIT 1

  SELECT company_exposition_target_discretionary_notes.* 
  FROM company_exposition_target_discretionary_notes 
  WHERE company_exposition_target_discretionary_notes.company_exposition_target_id = 102 
  AND company_exposition_target_discretionary_notes.company_exposition_target_id = 102
  LIMIT 1
  SELECT company_exposition_targets.*
  FROM company_exposition_targets
  INNER JOIN company_exposition_target_discretionary_notes
  ON company_exposition_target_discretionary_notes.company_exposition_target_id = company_exposition_targets.id 
  AND company_exposition_target_discretionary_notes.company_exposition_target_id = NULL
它返回预期的结果-最后一个注释

我最初需要的是:

按最后一个任意注释对目标进行排序。属性

要进行排序,我要创建一个作用域:

scope :discretionary_notes_available, ->(true_false) {
  joins(:last_discretionary_note)# more data selection based on true_false
}
现在的问题是:
Company::ExpositionTarget.joins(:last\u decreditional\u note)
不返回任何内容

生成的SQL:

  SELECT  company_exposition_targets.*
  FROM company_exposition_targets
  WHERE company_exposition_targets.id = 102
  LIMIT 1

  SELECT company_exposition_target_discretionary_notes.* 
  FROM company_exposition_target_discretionary_notes 
  WHERE company_exposition_target_discretionary_notes.company_exposition_target_id = 102 
  AND company_exposition_target_discretionary_notes.company_exposition_target_id = 102
  LIMIT 1
  SELECT company_exposition_targets.*
  FROM company_exposition_targets
  INNER JOIN company_exposition_target_discretionary_notes
  ON company_exposition_target_discretionary_notes.company_exposition_target_id = company_exposition_targets.id 
  AND company_exposition_target_discretionary_notes.company_exposition_target_id = NULL
此查询错误,未考虑到
的定义has\u one:last\u decreditional\u note

Company::ExpositionTarget.find(102).last_discretionary_note
问题: 为什么AR不能正确处理范围内的has_one关联?还有,更重要的是——我的错误在哪里?我如何实现我所需要的?

同样,我需要以下信息:

scope :discretionary_notes_available, ->(true_false) {
  select all company_exposition_targets where company_exposition_targets.discretionary_notes.last.allow_participant_to_download_pdf = true_false
}

哦,真的吗,否决票?也许会告诉我为什么,沉默的家伙:)是啊,奇怪,又一次高估了你。。。对不起,我没有什么贡献:)你有什么AR版本?你有没有尝试过用更小的代码来复制它,类似于?@DmitryVolushkin我想当时我在Github上制造了一个问题,但AFAIR没有时间复制它,并找到了一些解决方法……哦,真的,向下投票?也许会告诉我为什么,沉默的家伙:)是啊,奇怪,又一次高估了你。。。对不起,我没有什么贡献:)你有什么AR版本?您是否尝试过用更小的代码复制它,类似于?@DmitryVolushkin我想当时我在Github上制造了一个问题,但AFAIR没有时间复制它,并找到了一些解决方法。。