我有三个 rails 模型:Usage(month, amount), Project(title, description) & Project_savings(amount)。
每个项目将有一个 project_saving.amount 与每个 usage.month 实例相关联。因此,每次使用将有_many project_savings(每个项目一个),每个项目将有_many project_savings(每个使用月一个)。每个项目的每个 project_saving.amount 等于该月的 usage.amount。
我试图这样做:
Project has_many :project_savings,
has_many :usages, through :project_savings
Usage has_many :project_savings
has_many :projects, through :project_savings
Project_saving belongs_to :usage, :project
我会将计算代码的代码放在 project_savings 模型中吗?如果我将以下内容放入我的 ProjectSaving 模型中:
Project.all.each do |p|
Usage.all.each do |u|
p.create_project_saving(amount: u.amount)
end
end
这会将usage_id和project_id自动关联到project_saving,还是我需要手动输入p.create_project_saving(usage_id:u.id, amount:u.amount)
?
干杯!