Forum: Ruby on Rails habtm and options_from_collection_for_select [1.0.0]

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Paul M. (Guest)
on 2006-04-04 22:06
(Received via mailing list)
I ran into a problem using options_from_collection_for_select :

ActiveRecord::AssociationTypeMismatch (Topic expected, got String):
active_record/associations/association_collection.rb:114:in `replace'
active_record/associations/association_collection.rb:114:in `replace'
active_record/associations.rb:770:in `topics='

My models:

class Company < ActiveRecord::Base
   has_and_belongs_to_many :topics

class Topic < ActiveRecord::Base
   has_and_belongs_to_many :companies

And the form elements:

<select id="company_topics" name="company[topics][]"
   <%= options_from_collection_for_select(Topic.find_all, "id",
"name") %>

I tested using the connsole, and things work as expected.

Any help greatly appreciated !

Chris H. (Guest)
on 2006-04-04 22:58
(Received via mailing list)
i don't think this is an error related to

the key in the error is:


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
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"]

This topic is locked and can not be replied to.