简体   繁体   English

如何使用Rails在Postgres中存储属性?

[英]How to store attributes in Postgres with Rails?

I have a model called Page. 我有一个名为Page的模型。 The users are able to specify attributes whenever they create a Page. 用户可以在创建页面时指定属性。 Ideally, they will choose from a list of attributes and assign them to the Page. 理想情况下,他们将从属性列表中进行选择并将其分配给页面。

My first though was to create as many columns as attributes the user is able to select, and basically marking them as true/false if it has been selected. 我的第一个操作是创建与用户可以选择的属性一样多的列,如果已选择,则基本上将它们标记为true / false。

However, I have a feeling that I might be missing a better approach. 但是,我感觉我可能会缺少更好的方法。 I have also used at HSTORE, but at the end of the day, I would also need to do something like: 我也曾在HSTORE使用过,但最终,我还需要做一些类似的事情:

attributes: { 'attribute1' => 'true', 'attribute2' => 'false' } and I am not sure if that is appropriate. attributes: { 'attribute1' => 'true', 'attribute2' => 'false' } ,但我不确定是否合适。

I will use those attributes in order to select pages, so I should be able to say something like: 我将使用这些属性来选择页面,因此我应该能够说出以下内容:

Page.where(attribute1 is true).where(attribute2 is false).where(...)

How should I store those attributes in my Page model? 如何将这些属性存储在Page模型中?

The acts_as_taggable_on gem might be of help to you. actions_as_taggable_on宝石可能会对您有所帮助。

You would declare your model as taggable on your attributes as follows: 您将在属性上将模型声明为可标记的,如下所示:

class Page < ActiveRecord::Base
  acts_as_ordered_taggable_on :attributes
end

And your example query would be something like this: 您的示例查询将如下所示:

Page.tagged_with(["attribute1"]).tagged_with(["attribute2"], :exclude => true)

The Gem is very well documented here . 宝石在这里有很好的记载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM