"Expected Association Got String" with Many-to-Many Association

I am currently creating something like a “sports and results management application” in Rails 3. In this application I need to create several exercises that can have several “types of results” (heart rate, distance in km, rehearsals, ... ) And it should be possible to arrange the results in my preferred order. So this is a classic many-to-many relationship.

The following migrations appeared:

class CreateExercises < ActiveRecord::Migration def self.up create_table :exercises do |t| t.integer :user_id t.string :name t.text :beschreibung t.integer :resulttype_id t.boolean :active, :default => true t.timestamps end end def self.down drop_table :exercises end end class CreateResulttypes < ActiveRecord::Migration def self.up create_table :resulttypes do |t| t.string :name t.string :einheit t.text :beschreibung t.timestamps end end def self.down drop_table :resulttypes end end class CreateExercisesResulttypesJoin < ActiveRecord::Migration def self.up create_table :exercises_resulttypes, :id => false do |t| t.integer "exercise_id" t.integer "resulttype_id" end add_index :exercises_resulttypes, ["exercise_id", "resulttype_id"] end def self.down drop_table :exercises_resulttypes end end 

which works great with this format code:

  <%= semantic_form_for(@exercise) do |f| %> <%= f.inputs do %> <%= f.input :name %> <%= f.input :beschreibung %> <%= f.input :resulttype %> <%= f.input :active %> <% end %> <%= f.buttons %> <% end %> 

The problem is that with this code I cannot sort the types of results, and each exercise can have only one instance of resulttype. So I changed the formtastic: resulttype form for this code (except for jquery code):

  <div id="conn_ctrl" class="float_left center"> <div class="float_left center"> <%= f.select :resulttypes, @resall.collect{|d| [d.name,d.id]}.sort, {}, { :multiple => true, :class => "conn_select" } %><br> </div> <div class="float_left center"> <%= f.select :resulttypes, '', {}, { :multiple => true, :id => "exercise_resulttypes_save", :class => "conn_select" } %><br> </div> <div style="clear: both"></div> <%= tag("input", { :type => "button", :id => "remove", :class => "dualbuttons", :name => "", :value => "<<" }, false) %> <%= tag("input", { :type => "button", :id => "add", :class => "dualbuttons", :name => "", :value => ">>" }, false) %> <%= tag("input", { :type => "button", :id => "up", :class => "dualbuttons", :name => "", :value => "Up" }, false) %> <%= tag("input", { :type => "button", :id => "down", :class => "dualbuttons", :name => "", :value => "Down" }, false) %><br> </div> <div style="clear: both"></div> 

Now I have a Double List, and I can add exercises to this particular workout several times and order it as I like. BUT, when I submit the form, I get this error, and I have no idea what this means:

The result (# - 614051528) is expected to receive a string (# - 608366078)

Presented options:

  {"commit"=>"Create Exercise", "authenticity_token"=>"v1l9zfxdxIJbdjZx6SsZ5tGuKMrlioBg+C9orSmVarA=", "utf8"=>"✓", "exercise"=>{"name"=>"dddd", "beschreibung"=>"ddddd", "resulttypes"=>["5", "3", "1"], "active"=>"1"}} 

Currently, I'm a little crazy. I hope you know why this is happening ...

+4
source share
1 answer

This is a typical mistake: to set identifiers you must use setter association_ids= , Rails expects an array of objects for association= . Therefore, you should write:

 <%= f.select :resulttypes_ids, .... %> 
+5
source

Source: https://habr.com/ru/post/1338862/


All Articles