2014-02-10 17 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)>' 

ich mit der update_remaining_nutrients Methode auf status.rb anrufen möge, wann immer eine Mahlzeit „gefressen“ wird durch den Benutzer. Wenn ich User.first.eat (Meal.first) rufe, erhalte ich den ArgumentError. Nicht sicher, warum, weil ich die after_add-Methode keine Argumente gebe?Argument: falsche Anzahl von Argumenten (1 0), wenn afer_save

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 

Antwort

4

Wenn Sie einen Blick auf diehabenAbschnitt der docs, werden Sie in diesem Beispiel sehen:

class Project 
    has_and_belongs_to_many :developers, after_add: :evaluate_velocity 

    def evaluate_velocity(developer) 
    ... 
    end 
end 

Das ist nicht die has_many Beziehung, die Sie haben, aber es ist nahe genug. Wenn Sie sich die evaluate_velocity-Methode ansehen, werden Sie sehen, dass der fragliche developer als Argument vom :after_add-Callback übergeben wird. Sie bekommen einen ArgumentError über Ihre update_remaining_nutrients mit einem Argument aufgerufen, wenn es nicht will und das entspricht, was das Beispiel vorschlägt.

Versuchen Sie folgendes:

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