【问题标题】:Dynamic Forms with HStore and rails 4带有 HStore 和 rails 4 的动态表单
【发布时间】:2013-10-14 20:14:31
【问题描述】:

我关注了this railscast,并完成了教程。一切正常。然后我决定用hstore代替序列化hash,设置hstore后,遇到了错误:

PG::Error: ERROR: Syntax error near '!' at position 4 : INSERT INTO "products" ("product_type_id", "created_at", "properties", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"

我用谷歌搜索,找到了similar SO question,但我使用的是 Rails 4,据说它不再需要使用那个 gem。

这是我的代码:

我的 form.html.haml 的相关部分如下所示

= f.fields_for :properties, OpenStruct.new(@product.properties) do |builder|
  - @product.product_type.products.each do |product|
    = render "products/fields/#{product.field_type}", field: field, f: builder

我的产品模型如下所示:

class Product < ActiveRecord::Base
  belongs_to :product_type
  serialize :properties
end

如果有帮助,我可以发布更多代码。谢谢!

【问题讨论】:

  • Rails4 中的 hstore 是否必须使用serialize?我没有使用它,但我认为 Rails4 PostgreSQL 驱动程序本身就可以理解 hstore,因此不再需要 serialize kludgery。错误消息中的 ! 将显示在 YAML 中,这就是 serialize 默认使用的内容。
  • @muistooshort,这实际上解决了问题。你能把它作为答案让我接受吗

标签: ruby-on-rails ruby postgresql ruby-on-rails-4 hstore


【解决方案1】:

用于 ActiveRecord 的 Rails4 PostgreSQL 驱动程序应该具有对 PostgreSQL 的 hstore 类型的本机支持,因此您根本不需要使用 serialize。尝试放弃serialize

顺便说一句,当您尝试将某些对象序列化为 YAML 时,! 将出现在 YAML 字符串中:

"--- !ruby/object:SomeClassName ..."

如果 PostgreSQL 期望看到 hstore 字符串,! 可能会导致一些问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-31
    • 1970-01-01
    • 2012-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多