如何增加rails gem中的字段?

我收到这个错误:

SQL (31.1ms) INSERT INTO "read_marks" ("readable_id", "readable_type", "timestamp", "user_id") VALUES ($1, $2, $3, $4) RETURNING "id" [["readable_id", nil], ["readable_type", "PublicActivity::ORM::ActiveRecord::Activity"], ["timestamp", Mon, 04 Mar 2013 03:29:52 UTC +00:00], ["user_id", 2]] PG::Error: ERROR: value too long for type character varying(20) 

因为“readable_type”只包含20个字符,所以我传入“PublicActivity :: ORM :: ActiveRecord :: Activity”。

这与使用gem未读的public_activity (显然解决了问题,但没有说明如何(见他的底部更新))相同的问题

为什么不进行迁移并将readable_type更改为更长的类型? 像这样的东西:

 change_column :read_marks, :readable_type, "varchar(255)" 

或者如果你想更长时间:

 change_column :read_marks, :readable_type, :text 

由于您使用的是PostgreSQL,因此使用“无限”长度类型没有任何缺点,例如:text

从您添加的链接:

 UPDATE AH! It was the readable_type field having a limit of only 20 that was truncating my class name. Now it works after I increased the size. 

在数据库上,将列“readable_type”的大小增加到大于20的大小