相关文章
# 出现N+1查询问题的代码
markets.includes(:currency).map{ |market|
market_type: Currency.find_by_code(ask_unit).coin_region
}问题重现
markets表没有一个外键是currencies表的外键,好在belongs_to有选项可以指向非id列的关联
或者说ActiveRecord不管你的表结构如何,你想要指定哪一列是"主键"去关联都行
belongs_to :currency, foreign_key: :ask_unit,
primary_key::code
markets.includes(:currency).map
参考链接
