0

我正在迁移到 Rails 3。我想我终于在测试和开发中完成了所有工作,但现在在生产中(实际上是在暂存中,Rails 不相信该环境存在)某个查询不起作用。似乎 :joins 现在被忽略了。

class Organisation
  # Maybe this is relevant, because it has :dongle in it too.
  LICENCE_TABLE_INCLUDES = [:organisation, :user, :owner_organisation, :profile, :dongle,
                            {:nested_licences => [:profile]} ]

  has_many :dongles

  def latest_received_licences_for_each_dongle
    Licence.find(:all,
                 :joins => [:dongle],
                 :include => LICENCE_TABLE_INCLUDES,
                 :conditions => { 'dongles.organisation_id' => id },
                 #  :group => 'dongles.id',       # broken
                 :order => 'dongles.name ASC, licences.created_at DESC').
      group_by {|l| l.dongle.name}.
      sort_by {|d,ls| d}.
      map {|d,ls| ls[0]}

    # Tried to modernise it a little - same results.
    #Licence.
    #  joins(:dongle).
    #  includes(LICENCE_TABLE_INCLUDES).
    #  where(:dongles => { :organisation_id => id }).
    #  order('dongles.name ASC, licences.created_at DESC').
    #  group_by {|l| l.dongle.name}.
    #  sort_by {|d,ls| d}.
    #  map {|d,ls| ls[0]}
  end
end

class Dongle
  has_many :licences
  belongs_to :organisation
end

class Licence
  belongs_to :dongle
end

调用它会导致错误:

Mysql2::Error: Unknown column 'dongles.organisation_id' in 'where clause': SELECT `licences`.* FROM `licences` WHERE `licences`.`parent_licence_id` IS NULL AND ((`dongles`.organisation_id = 143)) AND (`licences`.parent_licence_id IN (22,23))
mysql2 (0.2.18) lib/active_record/connection_adapters/mysql2_adapter.rb:265:in `query'

如您所见,它没有将联接添加到查询中。(rails 3.0.17,mysql2 0.2.18 - 尚未更新到 Rails 3.2,因为我正试图让 3.0 首先工作。)

4

0 回答 0