开发者

difference in Rails 3 joins vs Rails 2

In migrating our rails2 app to rails3 we have come across (another!) subtle difference. Personally, it looks to me like the sql being generated by joins is wrong.

Given a model that looks like this:

class Event < ActiveRecord::Base
   has_many :event_event_categories
   has_many :locations, :class_name => "LocationCategory", :through => :event_event_categories, :source => :event_category, :uniq => true
   ...
end

we do some scopes that need to use locations for some columns. But the queries are subtly different in Rails 2 vs rails 3

Rails 2:

ree-1.8.7-2011.03 :037 > Event.joins(:locations).to_sql
 => "SELECT `events`.* FROM `events`   
       INNER JOIN开发者_Python百科 `event_event_categories` ON (`events`.`id` = `event_event_categories`.`event_id`)  
       INNER JOIN `event_categories` ON (`event_categories`.`id` = `event_event_categories`.`event_category_id`) AND  (`event_categories`.`type` = 'LocationCategory' )  "

Rails 3:

ree-1.8.7-2011.03 :037 > Event.joins(:locations).to_sql
 => "SELECT `events`.* FROM `events` 
     INNER JOIN `event_event_categories` ON `events`.`id` = `event_event_categories`.`event_id` 
     INNER JOIN `event_categories` ON `event_categories`.`type` = 'LocationCategory'" 

It is not putting the

(`event_categories`.`id` = `event_event_categories`.`event_category_id`)

into the sql.

Why is this?


In Rails3 you can use Queries like

 @alternative_drugs = Claim.joins('left join drugs on claims.ndc = drugs.ndcupchri ').select('claims.id, claims.member_id, claims.ndc').where("claims.nabp in ('1','2')").group(:ndc).order('calculated_price asc').all
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜