Я немного не уверен, как спросить это, поэтому я прошу прощения за неуклюжее объяснение.
У меня есть три модели: пользователь, Waterusage и цели
class Goal < ApplicationRecord
belongs_to :user
end
class Waterusage < ApplicationRecord
belongs_to :user
end
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable,
has_one :waterusage, :dependent => :destroy
has_one :goals, :dependent => :destroy
end
Waterusage заполняется сначала пользователями, а затем целями. Цели - это та же схема, что и у waterusage, но она использует часть формы waterusage и копирует остальные атрибуты из waterusage.
class Goal < ApplicationRecord
belongs_to :user
# before_validation :get_from_waterusage
before_validation :calculate_totals
def get_from_waterusage
self.household_size = @waterusage.household_size
self.swimming_pool = @waterusage.swimming_pool
self.bathroom_sink_flow_rate = @waterusage.bathroom_sink_flow_rate
self.low_flow_toilet = @waterusage.low_flow_toilet
self.kitchen_sink_usage = @waterusage.kitchen_sink_usage
self.kitchen_sink_flow_rate = @waterusage.kitchen_sink_flow_rate
self.dishwasher_rate = @waterusage.dishwasher_rate
self.dishwasher_multiplier = @waterusage.dishwasher_multiplier
self.laundry_rate = @waterusage.laundry_rate
self.laundry_multiplier = @waterusage.laundry_multiplier
self.lawn_size = @waterusage.lawn_size
self.carwash_rate = @waterusage.carwash_rate
self.carwash_multiplier = @waterusage.carwash_multiplier
self.miles = @waterusage.miles
self.statewater = @waterusage.statewater
self.percent_statewater = @waterusage.percent_statewater
self.pet_cost = @waterusage.pet_cost
end
...
end
Вот GoalsController
класс GoalsController
def new
@goal = goal.new
end
def create
#@user = User.find(params[:user_id])
@goal = current_user.create_goal(goal_params)
redirect_to goal_result_path
end
def destroy
@user = User.find(params[:user_id])
@goal = @user.goal.find(params[:id])
@goal.destroy
redirect_to user_path(current_user)
end
def show
@goal = goal.find(params[:id])
end
def results
if current_user.goal.get_individual_total > 6000
@temp = 6000
else
@temp = current_user.goal.get_individual_total
end
@goal = current_user.goal
end
private
def goal_params
params.require(:goal).permit(:household_size, :average_shower,
:shower_flow_rate, :bath_rate, :bath_multiplier,
:bathroom_sink_usage,
:bathroom_sink_flow_rate, :mellow, :low_flow_toilet,
:kitchen_sink_usage,
:kitchen_sink_flow_rate, :dishwasher_rate,
:dishwasher_multiplier,
:dishwasher_method, :laundry_rate, :laundry_multiplier,
:laundry_method,
:greywater, :lawn_rate, :lawn_multiplier, :lawn_size,
:xeriscaping,
:swimming_pool, :swimming_months, :carwash_rate,
:carwash_multiplier,
:carwash_method, :miles, :statewater, :percent_statewater,
:shopping,
:paper_recycling, :plastic_recycling, :can_recycling,
:textile_recycling,
:diet, :pet_cost, :individual_total, :household_total,
:home_usage, :outdoor_usage,
:individualDifference, :householdDifference, :vehicle_usage,
:power_usage, :indirect_source_usage,
:individualDifference, :householdDifference)
end
end
У меня сейчас следующая ошибка:
NameError in GoalsController#create
undefined local variable or method `current_user' for #
<Goal:0x007fbedde9a590>
Похоже, я получаю информацию из модели waterusage с помощью
self.household_size = @waterusage.household_size
Это объединение, которое я мог бы использовать?
Модель водного использования работает BTW.
Спасибо