开发者

why am I getting 'wrong number of arguments (2 for 1)' after upgrading from rails 2.0 to rails 2.3.3?

开发者 https://www.devze.com 2022-12-13 06:13 出处:网络
This is happening in an after_save method in one of my models, PlayerAction. I didn\'t change any code during the upgrade (famous last words). It appears to be happening on the following line:

This is happening in an after_save method in one of my models, PlayerAction. I didn't change any code during the upgrade (famous last words). It appears to be happening on the following line:

if self.action_type.id == 1

The PlayerAction model has a

belongs_to :action_type

Changing action_type.id to action_type_id fixed that problem, but the same problem is happening in another place, in this line

player_action.action_type.desc

Here is the full stack trace, where the problem is occurring in that line

C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/associations/belongs_to_association.rb:49:in `find'
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/associations/belongs_to_association.rb:49:in `send'
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/associations/belongs_to_association.rb:49:in `find_target'
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/associations/association_proxy.rb:240:in `load_target'
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/associations/association_proxy.rb:112:in `reload'
C:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/associations.rb:1219:in `action_type'
C:/InstantRails2-0/rails_apps/rftg/app/models/game.rb:164:in `full_xml'
C:/InstantRails2-0/rails_apps/rftg/app/models/game.rb:160:in `each'
C:/InstantRails2-0/rails_apps/rftg/app/models/game.rb:160:in `full_xml'
C:/InstantRails2-0/rails_apps/rftg/app/models/game.rb:156:in `full_xml'
C:/InstantRails2-0/rails_apps/rftg/app/models/game.rb:136:in `full_xml'
C:/InstantRails2-0/rails_apps/rftg/app/models/game.rb:86:in `full_xml'
C:/InstantRails2-0/rails_apps/rftg/app/controllers/games_controller.rb:33:in `show'
C:/InstantRails2-0/rails_apps/rftg/app/controllers/games_controller.rb:30:in `show'

ActionType is another model of mine, 开发者_开发知识库and is a pretty generic name, as pointed out in another answer. Could that be the source of the problem? I looked in the main rails framework docs and didn't see a class or method named ActionType or action_type.

Turns out I did just add a custom find method for ActionType:

def self.find(id)
  Rails.cache.fetch(cache_key) { ActionType.find(id) }
end 

ActionType is basically static data and I'm trying to cache it. There are about 10 rows for ActionType, and I have several other models that have similar static data. That's the whole reason I started the process to upgrade rails, to get that caching functionality. I've been trying to find an example of how to do this correctly. Clearly the above code isn't correct. I found another question here about the best-practice way to handle static data in a model, but it didn't quite help.


try it with

if self.action_type_id == 1

At least you'll know if that's the problem.

Okay, seeing that that is a problem, per comments, the line in Rails it's dying on is in here:

def find_target
      find_method = if @reflection.options[:primary_key]
                      "find_by_#{@reflection.options[:primary_key]}"
                    else
                      "find"
                    end
      @reflection.klass.send(find_method,
        @owner[@reflection.primary_key_name],
        :select => @reflection.options[:select],
        :conditions => conditions,
        :include => @reflection.options[:include],
        :readonly => @reflection.options[:readonly]
      ) if @owner[@reflection.primary_key_name]
    end

I am assuming you are using "id" for the primary key, because it is dying in the find method which is reflectively invoked. Of course, that method takes (*args), so shouldn't be dying on the 2 for 1 problem...

So, a) have you defined some custom finder for this class (finder_sql?)

b) what is the full_xml method that this is in? Is that just the part where you call

 player_action.action_type.desc

?

0

精彩评论

暂无评论...
验证码 换一张
取 消