Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在Rails中存储将在Hstore中使用的已定义属性?_Ruby On Rails_Postgresql_Hstore - Fatal编程技术网

Ruby on rails 如何在Rails中存储将在Hstore中使用的已定义属性?

Ruby on rails 如何在Rails中存储将在Hstore中使用的已定义属性?,ruby-on-rails,postgresql,hstore,Ruby On Rails,Postgresql,Hstore,我的模板模型上的一个属性称为设置,是HSTORE类型 我现在有以下几点: # Template model store_accessor :settings, :width, :height, :number_of_allowed_images, :is_ecommerce # View <%= f.fields_for :settings do |s| %> <%= f.text_field :width %>

我的模板模型上的一个属性称为设置,是HSTORE类型

我现在有以下几点:

# Template model
store_accessor :settings, :width, :height,
                          :number_of_allowed_images, :is_ecommerce


# View
<%= f.fields_for :settings do |s| %>
  <%= f.text_field :width %>
  <%= f.text_field :height %>
  <%= f.check_box :is_ecommerce %>
<%= end %>

# Controller

params.require(:template).permit(:name, settings: [:width, :height, :number_of_allowed_images, :is_ecommerce])
我刚刚展示了这4个设置属性,但我有大约80个。其中一些属性必须被视为文本字段,但其他属性是布尔值,例如电子商务

现在,我正在“静态”编写模型、视图和控制器中的属性,但我正在寻找一种方法来管理添加/删除/删除可用于设置的属性,考虑到并非所有属性都是同一类型,理想情况下,我希望他们中的一些有一个默认值,电子商务在默认情况下应该是假的


在处理HSTORE属性时如何执行这些操作?

HSTORE只能包含文本值,不支持默认值。听起来您应该将其建模为一个或多个关系,而不是与hstore。hstore的全部要点是,当您事先不知道属性时,它非常有用。如果您确实需要动态属性,但希望强制输入,那么您可能需要考虑存储json而不是hstore,因为它支持基本数据类型。在较新的PostgreSQL版本9.3和9.4中,对json索引和查询的支持越来越多。Craig Ringer感谢您的评论。有人建议我使用hstore,因为我有一个具有许多属性的表。现在我只有10个,但还会有更多。