ActiveRecord :: AssociationTypeMismatch for HABTM in rails

I have two models Zipcode and Plan

class Zipcode < ActiveRecord::Base
  self.primary_key = 'code'
  has_and_belongs_to_many :plans, :class_name => "Plan", 
     :join_table => "plans_zipcodes", :foreign_key => "code"
end

class Plan < ActiveRecord::Base
  has_and_belongs_to_many :zipcodes, :class_name => "Zipcode", 
    :join_table => "plans_zipcodes", :association_foreign_key => "code"
end

I use the code field (enter a string) as the primary key in the zipcodes table. In the connection table (plans_zipcodes), I have the plan_id and code fields.

Now when i try to do

plan = Plan.first
plan.zipcodes = [76546, 75001, 75667]

It gives me this

ActiveRecord::AssociationTypeMismatch: Zipcode(#90410810) expected, got Fixnum(#72919920)

Please help fix this.

+3
source share

All Articles