0

我正在将 rails3 应用程序升级到 3.2.6 我对已弃用的带有选项的 default_scope 有一些问题。

我有几个模型 Campaigns 和 Sites 通过campaign_sites 建立了多对多的关系。

class Campaign < ActiveRecord::Base
  has_many :campaign_sites, :dependent => :destroy
  ....
end

class Site < ActiveRecord::Base
  has_many :campaign_sites, :dependent => :destroy
  has_many :campaigns, :through => :campaign_sites
  ...
end

class CampaignSite < ActiveRecord::Base
  default_scope :include => [:campaign, :site], :order => :'sites.name' 
  belongs_to :campaign, :touch => true
  belongs_to :site, :touch => true
  ...
end

这在 rails 3.0.10 上运行良好,但在升级到 3.2.6 后,我在这一行中收到以下 mysql 错误。

default_scope :include => [:campaign, :site], :order => :'sites.name'

Mysql2::Error: Unknown column 'sites.name' in 'order clause': SELECT `campaigns`.* FROM`campaigns` INNER JOIN `campaign_sites` ON `campaigns`.`id` = `campaign_sites`.`campaign_id` WHERE `campaign_sites`.`site_id` = 11377 ORDER BY campaigns.name, sites.name 
4

1 回答 1

0

这可能会改变包含在 3.0.10 和 3.2.6 之间的工作方式。它现在不是进行单个查询,而是进行多个查询来进行预加载。我只想在其中添加一个joins选项,以便它明确加入站点表:

default_scope :include => [:campaign, :site], :order => :'sites.name', :joins => :sites
于 2012-07-02T12:15:21.920 回答