ArgumentError:使用afer_save时参数数目错误(1代表0)

光滑的
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_ Nutritions方法。当我调用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

饭菜

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
亩太短

如果您查看文档的“关联回调”部分,则会看到以下示例:

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回调作为回调传递给参数您将收到一个ArgumentError,该错误是关于您update_remaining_nutrients在不希望使用任何参数的情况下被调用的,并且与示例中所建议的匹配。

试试这个:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Rails:.destroy的参数数目错误(0表示1)

来自分类Dev

ArgumentError:使用afer_save时参数数目错误(1代表0)

来自分类Dev

Rails-错误的参数数量(1代表0)

来自分类Dev

Rspec参数数目错误(1代表0)

来自分类Dev

ArgumentError:Ruby中的参数数目错误

来自分类Dev

奇怪的参数数目错误(1代表0)错误?

来自分类Dev

参数数量错误(1代表0)Hartl第10章...捆绑exec rake db:populate

来自分类Dev

Rails jQuery POST-ArgumentError-参数数量错误(1代表0)?

来自分类Dev

Rails:.new的参数数目错误(1代表0)

来自分类Dev

Cocoon-错误的参数数量(1代表0)用于查找或创建:belongs_to

来自分类Dev

在'initialize'中:错误的参数数量(1代表2..3)(ArgumentError)

来自分类Dev

设计sign_in方法参数数量错误(1代表0)

来自分类Dev

多维数组-`initialize':错误的参数数量(1代表0)

来自分类Dev

工厂女孩说ArgumentError:参数数量错误(1代表2..3)

来自分类Dev

Rails 5升级:/actionpack-5.0.0/lib/action_controller/test_case.rb:49:in`initialize':错误的参数数量(0代表2)(ArgumentError)

来自分类Dev

Rails:参数数目错误(给定1,预期为0)

来自分类Dev

参数数量错误(0代表1)

来自分类Dev

送给Jekyll时参数数目错误(1对2)

来自分类Dev

参数数目错误(1代表0)(社交宝石)

来自分类Dev

Rails:.new的参数数目错误(1代表0)

来自分类Dev

Rails form_for错误-参数数目错误(0代表1)

来自分类Dev

ArgumentError输入的参数数目错误?方法

来自分类Dev

Rails:错误的参数数量(1代表0)

来自分类Dev

ArgumentError处的参数数目错误/错误(0表示2)

来自分类Dev

Rails错误的参数数量(1代表0),SubscribeController#update中的ArgumentError

来自分类Dev

`name':错误的参数数量(1代表0)(ArgumentError)

来自分类Dev

Rake不会回滚迁移,参数数量错误(1代表0)

来自分类Dev

错误:消息中的ArgumentError#个错误的新参数数目(2为1)

来自分类Dev

git push heroku master时参数数目错误(0为1)(ArgumentError)

Related 相关文章

  1. 1

    Rails:.destroy的参数数目错误(0表示1)

  2. 2

    ArgumentError:使用afer_save时参数数目错误(1代表0)

  3. 3

    Rails-错误的参数数量(1代表0)

  4. 4

    Rspec参数数目错误(1代表0)

  5. 5

    ArgumentError:Ruby中的参数数目错误

  6. 6

    奇怪的参数数目错误(1代表0)错误?

  7. 7

    参数数量错误(1代表0)Hartl第10章...捆绑exec rake db:populate

  8. 8

    Rails jQuery POST-ArgumentError-参数数量错误(1代表0)?

  9. 9

    Rails:.new的参数数目错误(1代表0)

  10. 10

    Cocoon-错误的参数数量(1代表0)用于查找或创建:belongs_to

  11. 11

    在'initialize'中:错误的参数数量(1代表2..3)(ArgumentError)

  12. 12

    设计sign_in方法参数数量错误(1代表0)

  13. 13

    多维数组-`initialize':错误的参数数量(1代表0)

  14. 14

    工厂女孩说ArgumentError:参数数量错误(1代表2..3)

  15. 15

    Rails 5升级:/actionpack-5.0.0/lib/action_controller/test_case.rb:49:in`initialize':错误的参数数量(0代表2)(ArgumentError)

  16. 16

    Rails:参数数目错误(给定1,预期为0)

  17. 17

    参数数量错误(0代表1)

  18. 18

    送给Jekyll时参数数目错误(1对2)

  19. 19

    参数数目错误(1代表0)(社交宝石)

  20. 20

    Rails:.new的参数数目错误(1代表0)

  21. 21

    Rails form_for错误-参数数目错误(0代表1)

  22. 22

    ArgumentError输入的参数数目错误?方法

  23. 23

    Rails:错误的参数数量(1代表0)

  24. 24

    ArgumentError处的参数数目错误/错误(0表示2)

  25. 25

    Rails错误的参数数量(1代表0),SubscribeController#update中的ArgumentError

  26. 26

    `name':错误的参数数量(1代表0)(ArgumentError)

  27. 27

    Rake不会回滚迁移,参数数量错误(1代表0)

  28. 28

    错误:消息中的ArgumentError#个错误的新参数数目(2为1)

  29. 29

    git push heroku master时参数数目错误(0为1)(ArgumentError)

热门标签

归档