ArgumentError:使用afer_save时参数数量错误(1表示0)

时间:2014-02-10 02:28:01

标签: ruby ruby-on-rails-3 rails-activerecord has-many

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方法的任何参数?

user.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

meal.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

1 个答案:

答案 0 :(得分:4)

如果您查看文档的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