Skip to content

Latest commit

 

History

History
40 lines (25 loc) · 1.13 KB

File metadata and controls

40 lines (25 loc) · 1.13 KB

相关文章

ActiveRecord关联的命名约定/规范

# 出现N+1查询问题的代码
markets.includes(:currency).map{ |market|
  market_type: Currency.find_by_code(ask_unit).coin_region
}

问题重现

markets表没有一个外键是currencies表的外键,好在belongs_to有选项可以指向非id列的关联

或者说ActiveRecord不管你的表结构如何,你想要指定哪一列是"主键"去关联都行

models/market.rb

belongs_to :currency, foreign_key: :ask_unit, primary_key: :code

usage.rb

markets.includes(:currency).map

association_without_primary_key_fix

参考链接