NoMethodError:未定义的方法''为nil:NilClass

时间:2017-10-20 01:17:59

标签: ruby-on-rails ruby

我有这些型号和控制器代码:

class Item < ActiveRecord::Base
 has_many :receivers_collateral_items, through: :received_trades, source: :wanted_item
 has_many :receivers_wanted_items, through: :received_trades, source: :collateral_item
 has_many :requesters_collateral_items, through: :requested_trades, source: :collateral_item
 has_many :requesters_wanted_items, through: :requested_trades, source: :wanted_item
 validates :year, :price, presence: true, numericality: true
 validates :shares, numericality: 
                     { only_integer: true, greater_than_or_equal_to: 0, 
                     less_than_or_equal_to: :build_shares }

 accepts_nested_attributes_for :receivers_collateral_items, :receivers_wanted_items, 
                               :requesters_collateral_items, :requesters_wanted_items, 
                               allow_destroy: true
end




class Trade < ActiveRecord::Base
 belongs_to :trade_requester, class_name: "User"
 belongs_to :trade_recipient, class_name: "User"
 belongs_to :wanted_item, class_name: "Item"
 belongs_to :collateral_item, class_name: "Item"
 validates :trade_requester, :trade_recipient, :wanted_item, :collateral_item, presence: true
 validates :shares, numericality: 
                     { only_integer: true, greater_than_or_equal_to: 0, 
                     less_than_or_equal_to: :max_shares }

 accepts_nested_attributes_for :wanted_item, :collateral_item, allow_destroy: true

 def max_shares
    if wanted_item.shares > collateral_item.shares
        collateral_item.shares
    else
        wanted_item.shares
    end
 end
end


class TradesController < ApplicationController
 def create
  @trade = current_user.requested_trades.build(trade_params)
 end

 private
  def trade_params
   params.require(:trade).permit(:trade_requester_id, :trade_recipient_id, :wanted_item_id, :collateral_item_id, :shares)
  end
end

但是我得到了NoMethodError: undefined method 'shares' for nil:NilClass和一个bd回滚。 重现错误的步骤:

t = Trade.new
t.trade_requester_id = User.find(1)
t.trade_recipient_id = User.find(2)
t.wanted_item_id = Item.second
t.collateral_item_id = Item.first
t.shares = 100
t.save

堆栈跟踪表明它来自numericality中的Trade验证,但在我看来,上要调用的类shares 存在。我应该可以致电t.wanted_item并获取该项目,但我得到nil。但是,t.wanted_item_id会返回该ID。这是为什么?

1 个答案:

答案 0 :(得分:2)

这可能是因为新记录缺少wanted_itemcollateral_item。如果这两个关联中的任何一个返回nil,则会抛出异常。要解决此问题,请使用旧版本的Ruby try或Ruby 2.3或更高版本的安全导航操作符(&.)。

这应解决您的问题(Ruby 2.3之前):

def max_shares
  if wanted_item.try(:shares).to_i > collateral_item.try(:shares).to_i
    wanted_item.shares
  else
    collateral_item.try(:shares).to_i
  end
end

try方法会返回wanted_item.sharescollateral_item.shares的值,如果它们不是nil。如果其中一个是nil,那么try将捕获异常并返回nilto_inil转换为零。

如果collateral_item.try(:shares).to_i存在,shares将返回collateral_item的值,如果collateral_itemnil,则返回零。

如果您使用的是Ruby 2.3或更高版本,则应将_item.try(:shares).to_i替换为_item&.shares.to_i。后者更清晰,faster in execution

<强>更新

对于复制,请确保将记录分配给关联而不是其ID:

t = Trade.new
t.trade_requester = User.find(1)
t.trade_recipient = User.find(2)
t.wanted_item = Item.second
t.collateral_item = Item.first
t.shares = 100
t.save!