2014-02-10 87 views
6
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方法?ArgumentError:当使用afer_save时参数数量错误(1代表0)

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 

回答

4

如果你有看该文档的部分,你会看到这个例子:

class Project 
    has_and_belongs_to_many :developers, after_add: :evaluate_velocity 

    def evaluate_velocity(developer) 
    ... 
    end 
end 

这不是你有has_many关系,但它是足够接近。如果您查看evaluate_velocity方法,您会看到问题作为参数由:after_add回调传递。当你不想要任何参数时,你会收到一个关于你的参数被调用的参数错误,这个参数与例子中的结果相符。

试试这个:

def update_remaining_nutrients(meal) 
    # Do interesting things with `meal` in here... 
end 
相关问题