This project is archived and is in readonly mode.
AssociationTypeMismatch, different versions of a class loaded
-
Robert Pankowecki
Could you share your view and controller code please ?
-
Matt D
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 endapp/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="✓" /><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
updateaction in the controller is still@job.update_attributes(params[:job]). (CanCan doesn't perform the update on its own inload_and_authorize_resourceas 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 theupdateaction. The error was thrown from that line, instead. This helps narrow down where the issue is by confirmed that it's the actualjob_title_idssetter 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
- 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
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
- No changes were found…
-
Rohit Arondekar
- State changed from new to resolved
- Importance changed from to Low