简单地运行
ElectricityProfile.find_each 做 |ep| 如果 UserProfile.exists?(ep.owner_id) && ep.owner_type == 'UserProfile' ElectricityProfileSummary.create(ep) 结尾 结尾
需要很长时间(5 小时)才能填充表格。有没有更好的方法来填充数据库?
假设从数据库中获取所有数据并将其存储在数组、哈希等中,然后推送以创建数据库
ElectricityProfile.find_each 做 |ep| 如果 UserProfile.exists?(ep.owner_id) && ep.owner_type == 'UserProfile' array_of_electricity_profiles.push(ep) 结尾 结尾 ElectricityProfileSummary.mass_create(ep) # => 或任何其他方法:)
抱歉忘了提我确实有覆盖方法create
,它需要多个模型并创建 ElectricityProfileSummary ...
创建!(:owner_id =>electricity_profile.owner_id, :owner_type =>electricity_profile.owner_type, :property_type =>electricity_profile.owner.user_property_type, :household_size =>electricity_profile.owner.user_num_of_people, :has_swimming_pool => 电力配置文件.has_swimming_pool, :bill => 电费账单, :contract =>electricity_profile.on_contract, :脏 => 真, :provider =>electricity_profile.supplier_id, :plan => 电力配置文件.plan_id, :状态=>状态, :邮政编码 => 邮政编码, :折扣 => 折扣, :has_air_conditioner => 电力配置文件.has_air_conditioner, :has_electric_hot_water => 电力配置文件.has_electric_hot_water, :has_electric_central_heating =>electricity_profile.has_electric_central_heat, :has_electric_cooktup => 电力配置文件.has_electric_cooktup )