Paul Mylchreest
2006-Apr-04 18:05 UTC
[Rails] habtm and options_from_collection_for_select [1.0.0]
I ran into a problem using options_from_collection_for_select : ActiveRecord::AssociationTypeMismatch (Topic expected, got String): /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/ active_record/associations/association_collection.rb:128:in `raise_on_type_mismatch'' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/ active_record/associations/association_collection.rb:114:in `replace'' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/ active_record/associations/association_collection.rb:114:in `replace'' /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/ active_record/associations.rb:770:in `topics='' My models: class Company < ActiveRecord::Base has_and_belongs_to_many :topics end class Topic < ActiveRecord::Base has_and_belongs_to_many :companies end And the form elements: <select id="company_topics" name="company[topics][]" multiple="multiple"> <%= options_from_collection_for_select(Topic.find_all, "id", "name") %> </select> I tested using the connsole, and things work as expected. Any help greatly appreciated ! Paul
Chris Hall
2006-Apr-04 18:34 UTC
[Rails] habtm and options_from_collection_for_select [1.0.0]
i don''t think this is an error related to options_from_collection_for_select. the key in the error is: /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/associations.rb:770:in `topics='' it''s clearly an association error related to topics if you look at the docs for habtm: collection=objects - replaces the collections content by deleting and adding objects as appropriate. so my guess in one of you''re actions you trying to set the company.topics to a string rather than a Topic object. a console test would confirm the same error company = Company.find(:first) test_params[:company][:topics] = ["1", "2", "3"] company.topics=test_params[:company][:topics] Chris On 4/4/06, Paul Mylchreest <paul.mylchreest@mac.com> wrote:> > I ran into a problem using options_from_collection_for_select : > > ActiveRecord::AssociationTypeMismatch (Topic expected, got String): > /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/ > active_record/associations/association_collection.rb:128:in > `raise_on_type_mismatch'' > /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/ > active_record/associations/association_collection.rb:114:in `replace'' > /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/ > active_record/associations/association_collection.rb:114:in `replace'' > /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/ > active_record/associations.rb:770:in `topics='' > > My models: > > class Company < ActiveRecord::Base > has_and_belongs_to_many :topics > end > > class Topic < ActiveRecord::Base > has_and_belongs_to_many :companies > end > > And the form elements: > > <select id="company_topics" name="company[topics][]" > multiple="multiple"> > <%= options_from_collection_for_select(Topic.find_all, "id", > "name") %> > </select> > > I tested using the connsole, and things work as expected. > > Any help greatly appreciated ! > > Paul > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060404/370b7679/attachment.html