【发布时间】:2013-05-06 09:06:11
【问题描述】:
我正在做以下查询
PortfolioDistribution.joins(:portfolio_distribution_split).
select('policy_number, reinvestment_date, reinvestment_units,
reinvestment_units, split_name, split_percentage').
where(:policy_number => policy_no, :reinvestment_date => period,
:ods_psgamas_gls_portfolio_distribution_split => { :split_name => type })
返回 15 条记录,但关联模型 portfolio_distribution_split 是 nil。如果我更改查询以包含关联模型,如下所示:
PortfolioDistribution.joins(:portfolio_distribution_split).
select('policy_number, reinvestment_date, reinvestment_units,
reinvestment_units, split_name, split_percentage').
where(:policy_number => policy_no, :reinvestment_date => period,
:ods_psgamas_gls_portfolio_distribution_split => { :split_name => type }).
includes(:portfolio_distribution_split)
我只得到 3 条记录?如果我使用生成的SQL 并直接针对数据库运行它,我会得到 15 条记录。其他 12 条记录怎么了?
- 活动模型 (3.2.13)
- 活动记录 (3.2.13)
- activerecord-sqlserver-adapter (3.2.8)
- activeresource (3.2.13)
- activesupport (3.2.13)
【问题讨论】:
-
你能在
where中标记我的位置吗?在那里你指定portfolio_distribution_split不应该为零?因为包含(对不起,如果我错了)使用其他join。请阅读关于 LEFT JOIN、INNER JOIN、RIGHT JOIN 的内容。
标签: activerecord ruby-on-rails-3.2 associations