簡體   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