manis
manis

Reputation: 729

Undefined method `stringify_keys' for 1:Fixnum

I receive an "NoMethodError: undefined method `stringify_keys' for 1:Fixnum, " error, any suggestions?

def self.gen_group(name, score, units = {})
  transaction do
    g = Group.create(name: name)
    units.each do |name, number|
      g.members.create(unit_id: Unit.find_by(name: name.to_s).id, amount: number)
    end
    g               
  end   
end

My method call:

g1 = Group.gen_group("Waldor's Slugs", :Pikeman => 5, :Archer => 4, :Wizard => 1)

Upvotes: 0

Views: 1172

Answers (2)

svoop
svoop

Reputation: 3454

The hash :Pikeman => 5, :Archer => 4, :Wizard => 1 is assigned to score (which is not used in the method) and not to units.

If your intention is to loop over the :Pikeman => ... hash in units.each, then just remove the obsolete score from the method signature.

Upvotes: 1

Lencho Reyes
Lencho Reyes

Reputation: 359

In your method call, try:

g1 = Group.gen_group("Waldor's Slugs", "Pikeman" => "5", "Archer" => "4", "Wizard" => "1")

I forget the details, but in a few places in Rails you can't rely on the symbol form (:Pikeman) being interchangeable with the string form ("Pikeman") in a hash. You can experiment with the method call to see if it's also necessary to send in the values as strings.

Upvotes: 0

Related Questions