default_scope with:include option rails 3.2

时间:2012-07-02 08:05:58

标签: ruby-on-rails associations upgrade default-scope

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

我有几个模型广告系列和网站,通过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 

1 个答案:

答案 0 :(得分:0)

这可能会改变包含在3.0.10和3.2.6之间的工作方式。现在,它不是进行单个查询,而是进行多次查询以进行急切加载。我只想在其中添加一个joins选项,因此它明确地加入了sites表:

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