This project is archived and is in readonly mode.

#5853 ✓resolved
Matt D

AssociationTypeMismatch, different versions of a class loaded

Reported by Matt D | October 21st, 2010 @ 09:56 PM

Relevant bits of relevant models:

class Job < ActiveRecord::Base
  has_and_belongs_to_many :job_titles

  attr_accessible :job_title_ids
end

class JobTitle < ActiveRecord::Base
  has_and_belongs_to_many :jobs
end

I have a form that sets job[job_title_ids]. On first submit, it acts as expected, saving the associations. On future submissions, I get error messages of type ActiveRecord::AssociationTypeMismatch along the lines of:

JobTitle(#84881490) expected, got JobTitle(#85299540)

These errors persist until I restart the server, or sometimes if I wait a few minutes, then I'm good for one more form submission. The number of the expected JobTitle class changes each refresh, whereas the second number remains constant.

I suspect it has to do with class reloading, and, if I cache classes, the issue ends. However, that's a messy workaround. Am I just missing something?

Comments and changes to this ticket

  • Robert Pankowecki

    Robert Pankowecki October 24th, 2010 @ 09:26 AM

    Could you share your view and controller code please ?

  • Matt D

    Matt D October 24th, 2010 @ 02:48 PM

    Making use of CanCan and Formtastic:

    app/controllers/jobs_controller.rb:

    class JobsController < ApplicationController
      before_filter :authenticate_user!, :require_user_has_a_company, :only => [:new, :create]
      load_and_authorize_resource :company
      load_and_authorize_resource :through => :company
    
      def index
        @jobs = Job.new_to_old.all :include => :company
      end
    
      def new
        @company = current_user.company
      end
    
      def create
        @job.company = current_user.company
        if @job.save
          redirect_to [@company, @job], :notice => "Job successfully saved"
        else
          render :action => :new
        end
      end
    
      def update
        if @job.update_attributes(params[:job])
          redirect_to [@company, @job], :notice => "Job successfully saved"
        else
          render :action => :edit
        end
      end
    end
    

    app/views/jobs/edit.html.haml:

    - title "Edit a job"
    = render 'form'
    

    app/views/jobs/_form.html.haml:

    = semantic_form_for [@church, @job] do |form|
      = form.inputs do
        = form.input :tagline, :hint => "Describe what you're looking for in one sentence"
        = form.input :job_titles, :as => :check_boxes
        = form.input :description
      = form.buttons do
        = form.commit_button 'Save job'
    

    HTML output of app/views/edit.html.haml:

    <form accept-charset="UTF-8" action="/churches/14/jobs/16" class="formtastic job" id="edit_job_16" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="_method" type="hidden" value="put" /><input name="authenticity_token" type="hidden" value="f/aSr9QKidvCVv0+83HoI7Za+6PdXPQBL3aQpR5KQGY=" /></div>
        <fieldset class="inputs"><ol><li class="string optional" id="job_tagline_input"><label for="job_tagline">Tagline</label><input id="job_tagline" maxlength="255" name="job[tagline]" size="50" type="text" value="Awesome job of awesome! :)" /> 
      <p class="inline-hints">Describe what you're looking for in one sentence</p></li> 
      <li class="check_boxes optional" id="job_job_titles_input"><fieldset><legend class="label"><label>Job titles</label></legend><input id="job_job_title_ids_" name="job[job_title_ids][]" type="hidden" value="" /><ol><li><label for="job_job_title_ids_3"><input id="job_job_title_ids_3" name="job[job_title_ids][]" type="checkbox" value="3" /> Big boss</label></li><li><label for="job_job_title_ids_4"><input checked="checked" id="job_job_title_ids_4" name="job[job_title_ids][]" type="checkbox" value="4" /> Secretary</label></li><li><label for="job_job_title_ids_1"><input checked="checked" id="job_job_title_ids_1" name="job[job_title_ids][]" type="checkbox" value="1" /> Code monkey</label></li></ol></fieldset></li> 
      <li class="text required" id="job_description_input"><label for="job_description">Description<abbr title="required">*</abbr></label><textarea cols="50" id="job_description" name="job[description]" rows="20">You better be awesome to get this job. Just saying.</textarea></li> 
      </ol></fieldset> 
      <fieldset class="buttons"><ol><li class="commit"><input class="update" id="job_submit" name="commit" type="submit" value="Save job" /></li> 
      </ol></fieldset> 
    </form>
    

    It's tempting to say that CanCan or Formtastic is the culprit, but Formtastic produces the form HTML I'd expect, and the relevant bit of the update action in the controller is still @job.update_attributes(params[:job]). (CanCan doesn't perform the update on its own in load_and_authorize_resource as it does for the initialization; I used the Rails logger to confirm.)

    I also, just for testing purposes, added @job.job_title_ids = ['', '1', '3'] to the top of the update action. The error was thrown from that line, instead. This helps narrow down where the issue is by confirmed that it's the actual job_title_ids setter that is the issue, and confirms that it doesn't have to do with the params object performing any strange magic.

    Thanks!

  • J.D. Hollis

    J.D. Hollis October 31st, 2010 @ 05:53 AM

    • Tag changed from 3.0.0, associations, has_and_belongs_to_many, reloading to 3.0.0, associations, default_scope, development, has_and_belongs_to_many, reloading

    Are you using default_scope anywhere? After much digging, I came across this ticket.

    Commenting out my uses of default_scope seems to have resolved this issue for me.

  • Matt D

    Matt D October 31st, 2010 @ 09:25 PM

    Wow, yes. Didn't expect that to be a factor.

    Good to see that there's an official patch, and that I'm not just totally going crazy. Ticket ready to be closed.

    Thanks!

  • Matt D

    Matt D October 31st, 2010 @ 09:25 PM

    • no changes were found...
  • Rohit Arondekar

    Rohit Arondekar December 12th, 2010 @ 02:26 AM

    • State changed from “new” to “resolved”
    • Importance changed from “” to “Low”

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages