2016-08-13 38 views
0

我必须处理这个错误,当我尝试将记录通过HABTM关联另一个相关联:“类型错误:的零到字符串的隐式转换”上HABTM协会

Person.first.communities = Communities.all 

模型和迁移:

class CreatePeople < ActiveRecord::Migration 
    def change 
    create_table :people do |t| 
     t.string :name 
     t.string :email 

     t.timestamps null: false 
    end 
    end 
end 

class CreateCommunities < ActiveRecord::Migration 
    def change 
    create_table :communities do |t| 
     t.string :name 
     t.text :description 

     t.timestamps null: false 
    end 
    end 
end 

class CreateJoinTablePersonCommunity < ActiveRecord::Migration 
    def change 
    create_join_table :people, :communities do |t| 
     # t.index [:person_id, :community_id] 
     # t.index [:community_id, :person_id] 
    end 
    end 
end 

我使用pg (0.18.4)宝石与下面的代码Postgres (9.5.2)

+0

您是否也可以显示相关联系的模型? – smefju

回答

0

佑康使用创建的关系。

Person.first.communities << Communities.all 

如果这不起作用,请通过模型上的反射关联方法检查您的关联。

相关问题