gpt4 book ai didi

ruby - 参数错误 : wrong number of arguments (1 for 0) when using afer_save

转载 作者:数据小太阳 更新时间:2023-10-29 06:59:56 24 4
gpt4 key购买 nike

ArgumentError: wrong number of arguments (1 for 0)
from /Users/Castillo/Desktop/gainer/app/models/status.rb:13:in `update_remaining_nutrients'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.11/lib/active_record/associations/collection_association.rb:507:in `block in callback'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.11/lib/active_record/associations/collection_association.rb:504:in `each'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.11/lib/active_record/associations/collection_association.rb:504:in `callback'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.11/lib/active_record/associations/collection_association.rb:352:in `add_to_target'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.11/lib/active_record/associations/collection_association.rb:495:in `block in concat_records'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.11/lib/active_record/associations/collection_association.rb:493:in `each'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.11/lib/active_record/associations/collection_association.rb:493:in `concat_records'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.11/lib/active_record/associations/collection_association.rb:134:in `block in concat'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.11/lib/active_record/associations/collection_association.rb:149:in `block in transaction'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.11/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.11/lib/active_record/transactions.rb:208:in `transaction'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.11/lib/active_record/associations/collection_association.rb:148:in `transaction'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.11/lib/active_record/associations/collection_association.rb:134:in `concat'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.11/lib/active_record/associations/collection_proxy.rb:116:in `<<'
from /Users/Castillo/Desktop/gainer/app/models/user.rb:65:in `eat'
from (irb):31
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.11/lib/rails/commands/console.rb:47:in `start'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.11/lib/rails/commands/console.rb:8:in `start'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/railties-3.2.11/lib/rails/commands.rb:41:in `<top (required)>'

每当用户“吃”一顿饭时,我想在 status.rb 上调用 update_remaining_nutrients 方法。当我调用 User.first.eat(Meal.first) 时,我得到了 ArgumentError。不确定为什么因为我没有向 after_add 方法传递任何参数?

用户.rb

class User < ActiveRecord::Base
has_many :meals
has_many :statuses

def eat(meal)
statuses.last.meals<<meal
end
end

status.rb

class Status < ActiveRecord::Base
attr_accessible :remaining_calories, :remaining_carbs, :remaining_protein, :weight, :user_id

belongs_to :user
has_many :meals, after_add: :update_remaining_nutrients

after_save :update_users_weight , :if => Proc.new {|a| a.weight_changed?}

def update_users_weight
self.user.weight.update_attributes(weight: self.weight)
end

def update_remaining_nutrients
puts "It Works!!!!!"
end

end

膳食.rb

class Meal < ActiveRecord::Base
attr_accessible :name, :description, :clean_up, :homemade, :prep_time, :user_id, :status_id

belongs_to :user
belongs_to :status
has_many :ingredient_meals
has_many :ingredients, :through => :ingredient_meals

end

最佳答案

如果您查看 Association callbacks文档的一部分,你会看到这个例子:

class Project
has_and_belongs_to_many :developers, after_add: :evaluate_velocity

def evaluate_velocity(developer)
...
end
end

这不是您拥有的has_many 关系,但它足够接近。如果您查看 evaluate_velocity 方法,您会看到相关的 developer 被作为参数传递给 :after_add 回调。你收到一个关于你的 update_remaining_nutrients 被调用的 ArgumentError,当它不需要任何参数时,它与示例建议的情况相匹配。

试试这个:

def update_remaining_nutrients(meal)
# Do interesting things with `meal` in here...
end

关于ruby - 参数错误 : wrong number of arguments (1 for 0) when using afer_save,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21668129/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com