【发布时间】:2011-10-25 22:08:45
【问题描述】:
一个 CertProgramItem has_many :cert_schedules。 A CertSchedule belongs_to :reg_fee_item, :foreign_key => 'reg_fee_item_id', :class_name => 'Item'
从 CertProgramItem 开始,我想在一个查询中获取所有 CertSchedules 及其相关表(以避免 n+1 问题)。我的第一个问题是:
cpi_arr = CertProgramItem.find(:all, :include => :cert_schedules, :order => :id)
但是,这并没有获取属于 CertSchedules 集合的 Item 类的成员。
我已经修改了查询:
cpi_arr = CertProgramItem.find(:all, :include => {:cert_schedules => :items}, :order => :id)
和
cpi_arr = CertProgramItem.find(:all, :include => {:cert_schedules => :reg_fee_items}, :order => :id)
但我收到诸如 ActiveRecord::ConfigurationError: Association named 'items' is not found 之类的错误;也许你拼错了?”或 ActiveRecord::ConfigurationError: Association named 'reg_fee_items' is not found;也许你拼错了?第二次。
有没有办法在一个查询中获得这种嵌套的外键关联?
【问题讨论】:
标签: ruby-on-rails