脚手架 ActiveRecord:相同数据类型的两列
另一个基本的 Rails 问题:
我有一个数据库表,需要包含对特定数据类型的两个不同记录的引用。
假设的例子:我正在制作一个视频游戏数据库。 我有一张“公司”表格。 我希望每个“视频游戏”条目都有一位开发商和一位发行商。
我知道,如果我想拥有一家公司,我可以这样做:
script/generate Videogame company:references
但我需要拥有两家公司。 我不想使用连接表,因为给定的数据类型只能有两种,而且我需要它们是不同的。
看起来答案应该很明显,但我在互联网上找不到它。
Another basic Rails question:
I have a database table that needs to contain references to exactly two different records of a specific data type.
Hypothetical example: I'm making a video game database. I have a table for "Companies." I want to have exactly one developer and exactly one publisher for each "Videogame" entry.
I know that if I want to have one company, I can just do something like:
script/generate Videogame company:references
But I need to have both companies. I'd rather not use a join table, as there can only be exactly two of the given data type, and I need them to be distinct.
It seems like the answer should be pretty obvious, but I can't find it anywhere on the Internet.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只是为了整理一下,在迁移中您现在还可以执行以下操作:
由于您调用键developer_id和publisher_id,模型可能应该是:
这不是一个主要问题,但我发现随着关联的数量随着额外参数的添加,事情变得越不清晰,因此最好尽可能坚持默认值。
Just to tidy things up a bit, in your migration you can now also do:
And since you're calling the keys developer_id and publisher_id, the model should probably be:
It's not a major problem, but I find that as the number of associations with extra arguments get added, the less clear things become, so it's best to stick to the defaults whenever possible.
我不知道如何使用脚本/生成来做到这一点。
无论如何,在不使用脚本/生成的情况下,基本思想更容易展示。 您需要视频游戏表/模型中的两个字段来保存公司表/模型的外键。
我将向您展示我认为代码的样子,但我还没有测试过它,所以我可能是错的。
您的迁移文件具有:
然后在您的模型中:
您还提到希望这两家公司是不同的,您可以在检查
developer_id !=publisher_id
的模型中的验证中处理这一点。I have no idea how to do this with script/generate.
The underlying idea is easier to show without using script/generate anyway. You want two fields in your videogames table/model that hold the foreign keys to the companies table/model.
I'll show you what I think the code would look like, but I haven't tested it, so I could be wrong.
Your migration file has:
Then in your model:
You also mention wanting the two companies to be distinct, which you could handle in a validation in the model that checks that
developer_id != publisher_id
.如果您想要特定于特定公司类型的任何方法或验证,您可以对公司模型进行子类化。 这采用了一种称为单表继承的技术。 有关更多信息,请查看这篇文章: http://wiki.rubyonrails.org/rails/pages/ singletableinheritance
然后您将拥有:
因此,您将拥有可供使用的 Company、Developer 和 Publisher 模型。
If there are any methods or validation you want specific to a certain company type, you could sub class the company model. This employs a technique called single table inheritance. For more information check out this article: http://wiki.rubyonrails.org/rails/pages/singletableinheritance
You would then have:
As a result, you would have Company, Developer and Publisher models to use.