Going Insane.... Getting "Type Mismatch Error" on Create & Update

Hello,

I am really pulling my hair out on this one and so would desperately
appreciate any help. The application I am building has Schools and
SchoolZones, both are in a HABTM relationship. On the School view
there is a list of checkbox for each corresponding SchoolZone that
School should belong to. When I submit, either for a Create and
Update action I get the following error.

ActiveRecord::AssociationTypeMismatch in SchoolsController#update
SchoolZone expected, got SchoolZone
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/
associations/association_proxy.rb:148:in raise_on_type_mismatch' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/ associations/association_collection.rb:141:in replace’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/
associations/association_collection.rb:141:in each' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/ associations/association_collection.rb:141:in replace’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/
associations.rb:962:in school_zones=' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/ associations.rb:972:in send’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/
associations.rb:972:in school_zone_ids=' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/ base.rb:1675:in send’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/
base.rb:1675:in attributes=' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/ base.rb:1674:in each’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/
base.rb:1674:in attributes=' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.5/lib/active_record/ base.rb:1594:in update_attributes’
/var/www/html/BostonConnects/app/controllers/schools_controller.rb:
57:in update' /usr/bin/mongrel_rails:16:in load’
/usr/bin/mongrel_rails:16

The really wierd thing is that this works on my local, which is a
Windows XP machine. The error only shows when I deploy it to our
Linux test environment. I posted the other code below in the hopes
that it helps. Any insight would be appreciated at this point… Many
thanks in advance. – Chad

class School < ActiveRecord::Base

Relationships

#has_and_belongs_to_many :users
has_and_belongs_to_many :services
has_and_belongs_to_many :school_zones
has_many :students
has_many :users

End Relationships

Properties

attr_accessor :email_address_confirmation

def school_zone_names
if @school_zones.nil?
school_zones
end

  s = ""
  i = 0
  for schoolzone in @school_zones
    s += i == 0 ? schoolzone.name : ", " + schoolzone.name
    i += 1
  end
  @school_zone_names = s

end

def principal_full_name
@principal_full_name = " #{principal_first_name}
#{principal_last_name}"
end

End Properties

Validators

validates_presence_of :name, :address_one, :city, :state, :zipcode
validates_confirmation_of :email_address,
:if => Proc.new{ |u| !u.email_address.blank? }
validates_format_of :zipcode,
:with => /^\d{5}([-|\s]?\d{4})?$/ix,
:if => Proc.new { |u| !u.zipcode.blank? }
validates_format_of :email_address,
:with => /^\S+@([?)[a-zA-Z0-9-.]+.([a-zA-Z]{2,4}|[0-9]{1,4})
(]?)$/ix,
:if => Proc.new { |u| !u.email_address.blank? }
validates_format_of :phone_one,
:with => /^(\d{3}-?\d{3}-?\d{4})(\s[a-zA-Z0-9\s.-#]{0,15})?$/
ix,
:message => “is invalid. Must be in the xxx-xxx-xxxx format.”,
:if => Proc.new { |u| !u.phone_one.blank? }
validates_format_of :phone_two,
:with => /^(\d{3}-?\d{3}-?\d{4})(\s[a-zA-Z0-9\s.-#]{0,15})?$/
ix,
:message => “is invalid. Must be in the xxx-xxx-xxxx format.”,
:if => Proc.new { |u| !u.phone_two.blank? }
validates_format_of :fax,
:with => /^(\d{3}-?\d{3}-?\d{4})(\s[a-zA-Z0-9\s.-#]{0,15})?$/
ix,
:message => “is invalid. Must be in the xxx-xxx-xxxx format.”,
:if => Proc.new { |u| !u.fax.blank?}

End Validators

Static Methods

def School.find_id_and_name
School.find(:all,
:select => “schools.id, schools.name”,
:conditions => “schools.active = 1”,
:order => “schools.name”)
end

def School.search(search_term, show_inactive, page, sort_order)
page_size = 50
if (search_term && search_term.empty? == false)
School.paginate(:per_page => page_size,
:page => page,
:conditions => [‘schools.active = ’ + show_inactive + ’ AND
schools.name LIKE ?’, “%#{search_term}%”],
:order => sort_order)
else
School.paginate(:per_page => page_size,
:page => page,
:conditions => 'schools.active = ’ + show_inactive,
:order => sort_order)
end
end

End Methods

end

class SchoolZone < ActiveRecord::Base

Relationships

has_and_belongs_to_many :schools

End Relationship

Static Methods

def SchoolZone.find_id_and_name()
SchoolZone.find(:all,
:select => “school_zones.id, school_zones.name”,
:conditions => “school_zones.active = 1”,
:order => “school_zones.name”)
end

End Static Methods

end

class SchoolsController < ApplicationController
helper :sort
include SortHelper
require “form_value”
helper :search
include SearchHelper

def index
list
render :action => ‘list’
end

GETs should be safe (see

URIs, Addressability, and the use of HTTP GET and POST)
verify :method => :post, :only => [ :create, :update ],
:redirect_to => { :action => :list }

def list
search = params[:search]
sort_init(‘name’, :table => ‘schools’)
sort_update
search_init
@schools = School.search(search_term, show_inactive,
params[:page], sort_clause)
end

def new
@school = School.new
init_form_values()
end

def create
@school = School.new(params[:school])
@school.updated_by_id = session[:user_id].to_i
init_form_values()

if @school.save
  flash[:notice] = 'The record for ' + @school.name + ' was

successfully created.’
redirect_to :action => ‘list’
else
render :action => ‘new’
end
end

def edit
@school = School.find(params[:id])
init_form_values()
end

def update
@school = School.find(params[:id])
@school.updated_by_id = session[:user_id].to_i
init_form_values()

if !params['school']['school_zone_ids']
  @school.school_zones.clear
end

if @school.update_attributes(params[:school])
        flash[:notice] = 'The record for ' + @school.name + ' was

successfully saved.’
redirect_to :action => ‘list’
else
render :action => ‘edit’
end
end

private
def init_form_values()
@zones = SchoolZone.find_id_and_name()
@states = FormValue.find_states()
end
end

School View:
<%= error_messages_for ‘school’ %>

<% if @school.new_record? %>





<% end%>

* School name: <%= form.text_field :name, :class => "tb", :maxlength => "50" %>
Principal First name: <%= form.text_field :principal_first_name, :class => "tb", :maxlength => "50" %>
Principal Last name: <%= form.text_field :principal_last_name, :class => "tb", :maxlength => "50" %>
Phone one: <%= form.text_field :phone_one, :class => "tb", :maxlength => "12" %>
Phone two: <%= form.text_field :phone_two, :class => "tb", :maxlength => "12" %>
Fax: <%= form.text_field :fax, :class => "tb", :maxlength => "12" %>
Email Address: <%= form.text_field :email_address, :class => "tb", :maxlength => "150" %>
Re-type
Email address:
<%= form.text_field :email_address_confirmation, :class =>
“tb”, :maxlength => “150” %>
Website: <%= form.text_field :website, :class => "tb", :maxlength => "150" %>
School zone: <% for school_zone in @zones %> checked="checked" <% end %> /> <%= school_zone.name %>
<% end %>
</td>
* Address one: <%= form.text_field :address_one, :class => "tb", :maxlength => "50" %>
Address two: <%= form.text_field :address_two, :class => "tb", :maxlength => "50" %>
* City: <%= form.text_field :city, :class => "tb", :maxlength => "50" %>
* State: <%= form.select(:state, @states.map{ |i| [i.label, i.value] }, {:prompt => true}, :class => "ddl" ) %>
* Zipcode: <%= form.text_field :zipcode, :class => "tb", :maxlength => "10" %>
Support Team Meeting Times:
Individual review
<%= form.text_field :support_meeting_notes, :class => "tb", :maxlength => "500" %>
<% if @school.new_record? || @school.support_meeting_times.nil? %> <%= form.radio_button :support_meeting_times, 1, :class => "rb", :checked => true %> <% else %> <%= form.radio_button :support_meeting_times, 1, :class => "rb" %> <% end %> NA <%= form.radio_button :support_meeting_times, 2, :class => "rb" %> Weekly <%= form.radio_button :support_meeting_times, 3, :class => "rb" %> Bi-Weekly
Whole class review
<%= form.text_field :class_meeting_notes, :class => "tb", :maxlength => "500" %>
<% if @school.new_record? || @school.class_meeting_times.nil?%> <%= form.radio_button :class_meeting_times, 1, :class => "rb",:checked => true %> <% else %> <%= form.radio_button :class_meeting_times, 1, :class => "rb" %> <% end %> NA <%= form.radio_button :class_meeting_times, 2, :class => "rb" %> Weekly <%= form.radio_button :class_meeting_times, 3, :class => "rb" %> Bi-Weekly
Notes: <%= form.text_area :notes, :style=>"width: 98%; height: 50px;", :class => "tb", :maxlength => "1000" %>
* Status: <%= form.radio_button :active, 'false', :class => "rb" %> Inactive <% if @school.new_record?%> <%= form.radio_button :active, 'true', :checked => true, :class => "rb" %> <% else %> <%= form.radio_button :active, 'true', :class => "rb" %> <% end %> Active

<%= form.hidden_field :created_on %>
<%= form.hidden_field :updated_on %>
<%= form.hidden_field :lock_version %>

DataBase Schema:
create_table “school_zones”, :force => true do |t|
t.column “name”, :string, :limit => 150, :default => “”, :null
=> false
t.column “active”, :integer, :default => 1, :null
=> false
end

create_table “school_zones_schools”, :id => false, :force => true do
|t|
t.column “school_id”, :integer, :null => false
t.column “school_zone_id”, :integer, :null => false
end

create_table “schools”, :force => true do |t|
t.column “name”, :string, :limit =>
150, :default => “”, :null => false
t.column “website”, :string, :limit => 150
t.column “principal_first_name”, :string, :limit => 50
t.column “principal_last_name”, :string, :limit => 50
t.column “email_address”, :string, :limit => 150
t.column “phone_one”, :string, :limit => 20
t.column “phone_two”, :string, :limit => 20
t.column “fax”, :string, :limit => 20
t.column “school_zone”, :integer
t.column “address_one”, :string, :limit =>
50, :default => “”, :null => false
t.column “address_two”, :string, :limit => 50
t.column “city”, :string, :limit =>
50, :default => “”, :null => false
t.column “state”, :string, :limit =>
2, :default => “”, :null => false
t.column “zipcode”, :string, :limit =>
10, :default => “”, :null => false
t.column “support_meeting_notes”, :string, :limit => 500
t.column
“support_meeting_times”, :integer, :default =>
1, :null => false
t.column “class_meeting_notes”, :string, :limit => 500
t.column
“class_meeting_times”, :integer, :default =>
1, :null => false
t.column “notes”, :string, :limit => 1000
t.column
“active”, :boolean, :default =>
true, :null => false
t.column
“updated_by_id”, :integer,
:null
=> false
t.column
“created_on”, :datetime,
:null
=> false
t.column
“updated_on”, :datetime,
:null
=> false
t.column
“lock_version”, :integer, :default =>
1, :null => false
end

On 5 Feb 2008, at 06:16, Chad wrote:

Hello,

I am really pulling my hair out on this one and so would desperately
appreciate any help. The application I am building has Schools and
SchoolZones, both are in a HABTM relationship. On the School view
there is a list of checkbox for each corresponding SchoolZone that
School should belong to. When I submit, either for a Create and
Update action I get the following error.

Last time I saw something like this it was due to a dependencies
problem: if the problem goes away if you run in production mode, then
the problem is that rails’ automatic class reloading is getting
confused. When i’ve seen this before, the problem has usually been
require statements of models etc…, the require bypassed the magic
class loading stuff and so things got confused. Either using
require_dependency or just removing the requires usually sorts it out.

Fred

Hi Fred,

Thanks for the reply. A few things: 1) When you say “Either using
require_dependency or just removing the requires usually sorts it
out.” Where should this be added or removed? I tried adding require
“school_zone” to the controller and that did not help. Could you be
very specific where you think the changes should be made and I will
test them out. And 2) It is not that is working in production mode,
rather is working on Windows machine but not the Linux server. Thanks
again!!

On Feb 5, 4:48 am, Frederick C. [email protected]

On 5 Feb 2008, at 17:12, Chad wrote:

Hi Fred,

Thanks for the reply. A few things: 1) When you say “Either using
require_dependency or just removing the requires usually sorts it
out.” Where should this be added or removed? I tried adding require
“school_zone” to the controller and that did not help. Could you be
very specific where you think the changes should be made and I will
I’d remove any require statement of models in your app.

Fred

Hi Fred,

No luck… Still getting the same message, I came accross another
thread AssociationTypeMismatch: RoleType expected, got RoleType - Rails - Ruby-Forum (that I think you
participated in) where it was suggested that the active record
association_proxy file that the raise_on_type_mismatch method be
changed to:

/activerecord-1.15.3/lib/active_record/associations/
association_proxy.rb:146

    def raise_on_type_mismatch(record)
      unless record.is_a?(eval(@reflection.class_name))
        raise ActiveRecord::AssociationTypeMismatch,

“#{@reflection.class_name} #{@reflection.klass} #{
record.is_a?(eval(@reflection.class_name)) } expected, got
#{record.class}”
end
end

I don’t have permission to change this file, but if you think it might
solve it I will work with the Sys Admin. What’s your sense? Also are
there any other workarounds that you know of. At this point it does
not need to be pretty, just work. I really appreciate your help!

On Feb 5, 12:34 pm, Frederick C. [email protected]

Just in case this is helpful to others, what fixed the issue for me
was just to reboot Mongrel. So after much loss of hair and sanity the
ol’ reboot did the trick. Thanks Fred for trying to help.