mysql多态关联_mysql – 带有rails多态关联的N 1查询

我有两个型号.

专辑

class Album < ActiveRecord::Base

has_one :post, as: :post_module, dependent: :destroy

end

帖子(有标题属性)

class Post < ActiveRecord::Base

belongs_to :post_module, polymorphic: true

end

这是我的模板

我尝试使用:includes和:避免N 1查询的引用.

def index

@albums = Album.includes(:post).references(:post).to_a

end

但似乎N 1查询仍然出现.这有什么问题?

SQL (0.2ms) SELECT `albums`.`id` AS t0_r0, `albums`.`product_num` AS t0_r1, `albums`.`created_at` AS t0_r2, `albums`.`updated_at` AS t0_r3, `posts`.`id` AS t1_r0, `posts`.`title` AS t1_r1, `posts`.`date` AS t1_r2, `posts`.`post_module_id` AS t1_r3, `posts`.`post_module_type` AS t1_r4, `posts`.`created_at` AS t1_r5, `posts`.`updated_at` AS t1_r6 FROM `albums` LEFT OUTER JOIN `posts` ON `posts`.`post_module_id` = `albums`.`id` AND `posts`.`post_module_type` = 'Album'

Post Load (0.3ms) SELECT `posts`.* FROM `posts` WHERE `posts`.`post_module_id` = 18 AND `posts`.`post_module_type` = 'Album' ORDER BY `posts`.`id` ASC LIMIT 1

Post Load (0.2ms) SELECT `posts`.* FROM `posts` WHERE `posts`.`post_module_id` = 20 AND `posts`.`post_module_type` = 'Album' ORDER BY `posts`.`id` ASC LIMIT 1

Post Load (0.2ms) SELECT `posts`.* FROM `posts` WHERE `posts`.`post_module_id` = 21 AND `posts`.`post_module_type` = 'Album' ORDER BY `posts`.`id` ASC LIMIT 1

Post Load (0.2ms) SELECT `posts`.* FROM `posts` WHERE `posts`.`post_module_id` = 22 AND `posts`.`post_module_type` = 'Album' ORDER BY `posts`.`id` ASC LIMIT 1

Post Load (0.1ms) SELECT `posts`.* FROM `posts` WHERE `posts`.`post_module_id` = 23 AND `posts`.`post_module_type` = 'Album' ORDER BY `posts`.`id` ASC LIMIT 1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值