繁体   English   中英

Rails:将模型属性设置为来自另一个模型的属性

[英]Rails: Setting Model Attributes to Attributes from Another Model

我不太确定如何提出这个问题,因此我为这个笨拙的解释表示歉意。

我有三个模型,用户,用水量和目标

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

用水首先由用户填写,然后是目标。 目标是与用水用法完全相同的架构,但是使用了一部分用水用法形式并复制了用水用法中的其余属性。

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 <ApplicationController before_action:authenticate_user!

  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>

这似乎是我从用水模型中检索信息的方式

self.household_size = @waterusage.household_size

有没有我可以使用的联接?

用水模型适用于BTW。

谢谢

不知道这是否是最好的方法,但是我会使用以下方法:

  • 在目标模型中,您可以检查其用户是否已经浪费水了。 如果有,则填写该用水量中的值

您可以使用after_initialize回调来实现。 在您的目标模型中,

class Goal < ApplicationRecord
  belongs_to :user
  after_initialize :set_default_values

  def set_default_values

    waterusage = self.user.try(:waterusage)
    if waterusage

        self.attribute1 = waterusage.attribute1
        self.attribute2 = waterusage.attribute2
        self.attribute3 = waterusage.attribute3
        #and it goes on...

    end

  end

end

因此,像这样在执行Goal.new ,它将检查该用户的用水情况并在目标上初始化这些值。 因此,您不必在控制器上进行任何更改,即使您在控制台上进行了更改,它也可以正常工作。 猜猜这是使用模型回调来做的更好的做法。 不知道它是否可以解决您的问题,请尝试一下。 祝好运!

您的错误消息是: NameError in GoalsController#create undefined local variable or methodNameError in GoalsController#create undefined local variable or method current_user'

current_user对象由您使用的Devise gem在控制器内部自动定义。 不会在模型中定义。

您的评论之一包括您在目标模型中使用的以下代码段: current_user.waterusage.household_size 这就是您的错误消息所指的。 (请注意,您的其中一个评论摘录与您原始帖子中的代码不同。这使您更难确定这里出了什么问题。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM