我有两个模特
和
拥有_和属于_Iongs_to_many: 项目的 用户
in project view I have a form that has a selector where I want to try 和assign multiple users to a project.
<%= select_tag("project[users]", options_for_select(@users, @project_users), {:multiple=>true, :size=>6} )%>
在我的工程控制器中, 选择中使用的变量是
@project_users = @project.users.collect { |user| user.id}
@users = User.all.collect { |user| ["#{user.first_name} #{user.last_name}", user.id] }
以显示
<select id="project_users" multiple="multiple" name="project[users][]" size="6">
<option value="#<User:0x007f567cb7f078>">User1</option>
<option value="#<User:0x007f567cb7e9c0>">User2</option>
</select>
问题是,这不等于
@some_project << [User(#), User(#)]
(注用户(#)代表类实例))
代之以其相当于
@some_project << ["1", "2"]
问题是用户实例被转换为字符串, 而不是再次返回到实例 。
Which does not work 和will throw an error as
ActiveRecord::AssociationTypeMismatch in ProjectsController#update
User(#70004716784160) expected, got String(#4266680)
我怎样才能使这个工作正确?