我正在做一个项目,在这个项目中,用户提交一个表单,并将其保存到一个waterusage模型中。然后他们需要在目标模型中制定目标。goals模型具有与waterusage模型相同的属性,因此创建waterusage对象时,我需要同时创建一个goals对象。然后,当用户返回到设置的目标,他们将得到一个子集的属性,他们可以编辑和比较他们的初始结果。
以下是模型:
class Waterusage < ApplicationRecord
belongs_to :user
before_validation :calculate_totals
def calculate_totals
self.home_usage = get_home_usage
self.outdoor_usage = get_outdoor_usage
self.vehicle_usage = get_vehicle_usage
self.power_usage = get_power_usage
self.indirect_source_usage = get_indirect_source_usage
self.household_total = get_household_total
self.individual_total = get_individual_total
end
def get_household_total
home_usage + outdoor_usage + vehicle_usage + power_usage + indirect_source_usage
end
def get_individual_total
household_total / household_size
end
def get_home_usage
shower_total + bath_total + bathroom_sink_total + toilet_total +
kitchen_total + dishwashing_total + laundry_total + greywater
end
def get_outdoor_usage
lawn_total + swimming_total
end
def get_vehicle_usage
(0.735 * miles) + carwash_total
end
def get_power_usage
statewater * percent_statewater / 100
end
def get_indirect_source_usage
(household_size*(shopping + paper_recycling + plastic_recycling + can_recycling + textile_recycling + diet)) + (200 * pet_cost / 30)
end
... (Insert many sub calculations here of attributes in the waterusages schema)
end
目标模型是相同的。
在goals控制器中,它需要使用与同一当前用户关联的waterusages示例创建一个新示例。
goals控制器需要独立于waterusage编辑其属性。
如何设定目标。new等于waterusage.new?在其中一个控制器里,模型?
1条答案
按热度按时间d8tt03nd1#
在你的
goals_controller.rb
,您可以执行以下操作:waterusage_params = waterusage.attributes.except('id', 'created_at', 'updated_at') # assuming that you have an instance of Waterusage goal = Goal.create(waterusage_params)