你将如何创建一个pre定义的一组属性

你将如何创建一个pre定义的一组属性

本文介绍了Rails模型:你将如何创建一个pre定义的一组属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出设计一个铁轨模型的最佳途径。为了这个例子,让我们说我要建字符的数据库,这可能有几个不同的固定属性。例如:

I'm trying to figure out the best way to design a rails model. For purposes of the example, let's say I'm building a database of characters, which may have several different fixed attributes. For instance:

Character
- Morality (may be "Good" or "Evil")
- Genre (may be "Action", "Suspense", or "Western")
- Hair Color (may be "Blond", "Brown", or "Black")

...等等。

... and so on.

因此​​,对于人物模型也有,我想主要有可能选择一个固定的名单几个属性。

So, for the Character model there are several attributes where I want to basically have a fixed list of possible selections.

我希望用户能够创建一个角色,并在窗体我希望他们能够选择一个来自每个可用的选项。我也希望能够让用户搜索使用这些属性...(即告诉我字符,这是'好',从'悬念'风格,并有棕色的头发)。

I want users to be able to create a character, and in the form I want them to pick one from each of the available options. I also want to be able to let users search using each of these attributes... ( ie, "Show me Characters which are 'Good', from the 'Suspense' genre, and have 'Brown' hair).

我能想到的几种方法可以做到这一点...

I can think of a couple ways to do this...

1:创建一个字符串为每个属性和验证有限的输入

在这种情况下,我会在字符表中定义一个字符串列道德,那么有一类常量,在它指定的选项,然后验证对那类常量。

In this case I would define an string column "Morality" on the character table, then have a class constant with the options specified in it, and then validate against that class constant.

找到好的角色会像 Character.where(:道德=>'好')

这是很好的,简单的,不足之处是,如果我想添加一些更详细的属性,比如有好与恶的描述,以及一个页面,用户可以浏览所有的字符一个给定的道德。

This is nice and simple, the downside is if I wanted to add some more detail to the attribute, for instance to have a description of "Good" and "Evil", and a page where users could view all the characters for a given morality.

2:为每个属性创建一个模型

在这种情况下,字符belongs_to的道德,将有一个道德模式与道德表有两个记录是:道德ID:1,名称:好

In this case Character belongs_to Morality, there would be a Morality model and a moralities table with two records in it: Morality id:1, name:Good etc.

找到好的角色会像 Morality.find_by_name(好)。字 ...或 Character.where(:道德=> Morality.find(1)

Finding good characters would be like Morality.find_by_name('Good').characters... orCharacter.where(:morality=> Morality.find(1).

这工作得很好,但它意味着你只存在持有少数predefined属性几个表。

This works fine, but it means you have several tables that exist only to hold a small number of predefined attributes.

3:创建STI模型的属性

在这种情况下,我可以做同样的#2,除了创建一个一般的CharacterAttributes表,然后继承它的MoralityAttribute和GenreAttribute等,这使得只有一个表的许多属性,否则它似乎大约相同的想法#2

In this case I could do the same as #2, except create a general "CharacterAttributes" table and then subclass it for "MoralityAttribute" and "GenreAttribute" etc. This makes only one table for the many attributes, otherwise it seems about the same as idea #2.

所以,那些是三种方式我能想到的解决这个问题。

So, those are the three ways I can think of to solve this problem.

我的问题是,你将如何实现这一点,为什么?

你会使用的方法之一以上,如果是哪一个?你会做不同的事情?我特别有兴趣听取性能考虑,你会采取的方法。我知道这是一个很宽泛的问题,谢谢你的任何输入。

Would you use one of the approaches above, and if so which one? Would you do something different? I'd especially be interested to hear performance considerations for the approach you would take. I know this is a broad question, thank you for any input.

编辑:我添加了对这个问题的250(我的名誉!超过10%)赏金,因为我真的可以使用的优点/缺点/选项更多一些广泛的讨论。我给upvotes任何人谁重与一些建设性的,如果有人可以给我他们采取哪种方法的一个非常坚实的例子,为什么它会是值得+250。

I'm adding a Bounty of 250 (more than 10% of my reputation!!) on this question because I could really use some more extended discussion of pros / cons / options. I'll give upvotes to anyone who weighs in with something constructive, and if someone can give me a really solid example of which approach they take and WHY it'll be worth +250.

我真的很折腾在我的应用程序的这方面的设计,它现在是时候来实现它。感谢您事先的任何有益的讨论!!

I'm really agonizing over the design of this aspect of my app and it's now time to implement it. Thanks in advance for any helpful discussion!!

最后要注意的:

感谢大家的深思熟虑和有趣的答案,所有的人都好,都对我很有帮助。最后(进来的权利之前赏金过期!)我真的AP preciated Blackbird07的答案。虽然每个人都提供了很好的建议,对我个人他是最有用的。我完全不知道枚举的想法面前,因为寻找到它,我发现它解决了很多我一直有我的应用程序的问题。我鼓励大家谁发现了这个问题,阅读所有的答案,还有提供了许多很好的做法。

Thank you all for your thoughtful and interesting answers, all of them are good and were very helpful to me. In the end (coming in right before the bounty expired!) I really appreciated Blackbird07's answer. While everyone offered good suggestions, for me personally his was the most useful. I wasn't really aware of the idea of an enum before, and since looking into it I find it solves many of the issues I've been having in my app. I would encourage everyone who discovers this question to read all the answers, there are many good approaches offered.

推荐答案

简单地说,你问如何枚举ActiveRecord的属性。有很多各地的网络讨论,甚至对因此对于使用枚举的Rails应用程序,如这里,here或这里仅举几例。

Put simply, you're asking how to enumerate ActiveRecord attributes. There are a lot of discussions around the web and even on SO for using enums in rails applications, e.g. here, here or here to name a few.

我从来没有使用过的众多的宝石之一,有对枚举,但是 active_enum宝石的声音特别适合您的使用案例。它没有一个ActiveRecord支持的属性集的缺点,使维护属性的值是小菜一碟。它甚至还带有形式帮手formtastic或简单的形式(我以为可以帮你的属性选择你的性格搜索)。

I never used one of the many gems there are for enums, but active_enum gem sounds particularly suited for your use case. It doesn't have the downsides of an activerecord-backed attribute set and makes maintenance of attribute values a piece of cake. It even comes with form helpers for formtastic or simple form (which I assume could help you for attribute selection in your character search).

这篇关于Rails模型:你将如何创建一个pre定义的一组属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 07:28