Rails3迁移,:现在忽略了连接,但我想将它用于条件

时间:2012-10-05 04:21:15

标签: ruby-on-rails ruby ruby-on-rails-3 join

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

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首先工作。)

0 个答案:

没有答案