- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我尝试通过 Rails 控制台更新 Puzzle
对象的 User
对象时,我得到一个数据库 ROLLBACK
并在开发中出现此错误:
类型错误:无法转换 ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Array::Data
只有当我尝试使用 update
(或 puzzle.user = some_user
之后的save
)时才会发生这种情况。添加初始所有者提交到数据库没有问题。
这里是架构中的模型:
create_table "users", force: :cascade do |t|
t.string "username"
t.string "password_digest"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "location_id"
end
create_table "puzzles", force: :cascade do |t|
t.string "name"
t.integer "pieces"
t.integer "missing_pieces"
t.string "previous_owners", array: true
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
到目前为止,这里是 puzzle.rb
和 user.rb
文件:
class User < ApplicationRecord
validates :username, presence: true
validates :email, presence: true#, uniqueness: true
# use bcrypt for password security
has_secure_password
has_many :puzzles
has_many :reviews
belongs_to :location
end
class Puzzle < ApplicationRecord
validates :name, uniqueness: true
validates :pieces, presence: true, numericality: { only_integer: true }
belongs_to :user
has_many :puzzle_tags
has_many :tags, through: :puzzle_tags
has_many :reviews
delegate :location, to: :user
end
知道是什么导致了这个问题吗?
***请注意:我是新手,第一次使用 PostgreSQL。我特别选择 Postgres 作为我的开发数据库而不是 SQLite3,因为它允许数组数据类型。谢谢!
最佳答案
您已将 previous_owners
设置为字符串数组,但您将整数压入其中。 ActiveRecord 通常擅长将字符串转换为整数,反之亦然,但从 Rails 5.1.5 开始,该功能在数组字段中不起作用。
尝试使用迁移将字段更改为整数数组。你需要做:
$ rails g migration change_previous_owners_to_integer_array
然后像这样编辑生成的迁移文件:
def change
remove_column :puzzles, :previous_owners, :string, array: true
add_column :puzzles, :previous_owners, :integer, array: true
end
关于ruby-on-rails - Rails 5.1.5:TypeError:无法转换 ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Array::Data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49437613/
我目前正在开发一个 sinatra 应用程序,我在 postgresql 与 sinatra 的连接方面遇到了问题,我尝试执行此命令: rake db:create 创建数据库但它抛出此错误。 C:\
我是 Ruby on Rails 的新手,但我已经学习了一些教程并且对自己的方法有了一些了解。我已经生成了一些脚手架并将数据插入到 MySql 数据库中。 导航到 index.html.erb 时,我
当我尝试运行迁移时,我收到以下信息: NoMethodError: undefined method `column' for # 这是迁移代码: class CreateAdvertisement
我已经使用 postgres 数据库创建了一个 Rails 应用程序。我正在使用 postgis 扩展进行地理查询。该应用程序在我的开发(本地)机器上成功运行,但是在我运行 heroku run ra
我有一个 Rails 5.1 应用程序,它使用 Devise 处理我的 User 模型的身份验证。这个应用程序有一个 Oracle 数据库后端,需要在执行任何查询之前为登录用户设置一个系统上下文变量,
生成了 rails 4.2.0 应用程序并添加了 activerecord-postgis-adapter 并相应地编辑了 database.yml 文件。 但是现在当我这样做时rake db:cre
我在运行 Rails 服务器时遇到问题。我在初始化程序中设置了 abstract_mysql2_adapters,包括: class ActiveRecord::ConnectionAdapters:
有两个应用程序。其中一个应用程序正在使用 rails 4.1.2。另一个应用程序正在使用 rails 5.0.1。以下是 rails 5 应用程序的工作方式:它检查用户是否已登录: 如果用户已登录:那
我目前正在将一个 Rails 项目从 v5.2.3 升级到 v6.0。我正在关注 guide升级 rails 。但我只停留在第一步。一旦我更改了 gemfile 中的 rails 版本并更新了 rai
我在基于测验的小型应用程序中使用 Active Admin gem。但是当我执行 耙数据库:迁移它给了我错误。以下是该命令的跟踪: $ rake db:migrate RAILS_ENV=produc
我们正在升级 Sinatra来自ActiveRecord 4的申请到 ActiveRecord 5。以前我们有这一行: 使用 ActiveRecord::ConnectionAdapters::Con
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
我们正在从 5.2 升级我们的 rails 应用程序至 6.0.1 ,现在每当我们运行 db:schema:load --trace我们得到以下错误。 ** Invoke db:schema:load
当我尝试通过 Rails 控制台更新 Puzzle 对象的 User 对象时,我得到一个数据库 ROLLBACK 并在开发中出现此错误: 类型错误:无法转换 ActiveRecord::Connect
我是一名优秀的程序员,十分优秀!