私は次のようなモデルを持っています
class Group < ActiveRecord::Base
end
class Plant < Group
has_many :plants_sub_plants, foreign_key: 'plant_id'
has_many :sub_plants, through: :plants_sub_plants
end
class PlantSubPlant < ActiveRecord::Base
belongs_to :plant, foreign_key: 'plant_id'
belongs_to :sub_plant, foreign_key: 'sub_plant_id'
end
class SubPlant < Plant
has_many :plants_sub_plants, foreign_key: 'sub_plant_id'
has_many :plants, through: :plants_sub_plants
end
移行:
class CreateTablePlantsSubPlants < ActiveRecord::Migration
def change
create_table :plants_sub_plants do |t|
t.belongs_to :plant
t.belongs_to :sub_plant
t.timestamps
end
add_index :plants_sub_plants, :plant_id
add_index :plants_sub_plants, :sub_plant_id
end
end
今、私が以下を行うと、爆発します。内部結合を呼び出さないのはなぜですか?
$Plant.first.sub_plants
Mysql2::Error: Unknown column 'groups.plant_id' in 'where clause': SELECT `groups`.* FROM `groups` WHERE `groups`.`type` IN ('SubPlant') AND `groups`.`plant_id` = 16
ActiveRecord::StatementInvalid: Mysql2::Error: Unknown column 'groups.plant_id' in 'where clause': SELECT `groups`.* FROM `groups` WHERE `groups`.`type` IN ('SubPlant') AND `groups`.`plant_id` = 16