如何使用 Scaffold 创建多对一关系?
我是 Ruby on Rails 的新手,我正在尝试创建一个低音吉他导师来自学 RoR(和低音吉他)。演练使用 Scaffold 创建 ActiveRecord 类,但它们似乎对应于独立的表;没有使用 belongs_to
或 has_many
。
我想创建三个类:Scale、GuitarString 和 Fret。每个音阶都有许多吉他弦,每个吉他弦都有许多音品。
如何使用 Scaffold 创建具有这种关系的类?有没有一种方法可以一次性完成,或者我是否需要使用 Scaffold 在不相关的状态下创建它们,然后手动添加关系?或者我应该完全放弃脚手架?
I'm new to Ruby on Rails, and I'm trying to create a bass guitar tutor in order to teach myself RoR (and bass guitar). The walkthroughs use Scaffold to create ActiveRecord classes, but they seem to correspond to standalone tables; there's no use of belongs_to
or has_many
.
I'd like to create three classes: Scale, GuitarString, and Fret. Each Scale has many GuitarStrings, which each have many Frets.
How do I create classes with this relationship using Scaffold? Is there a way to do it in one go, or do I need to create them in an unrelated state using Scaffold, then add the relations by hand? Or should I ditch Scaffold entirely?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
几周前我开始学习 Ruby on Rails,我发现通过不使用脚手架并从命令行(或宏中的宏)生成各个部分,掌握事物和学习方法要容易得多。 IDE)。
然而,据我所知,当您使用脚手架生成事物时,您会将其视为生成“资源”,因此您一次只会创建一个资源,然后手动添加关系。
但是,生成模型命令可以为您创建这些关系。假设您使用脚手架来创建 Scale 资源。
然后,您可以执行
The scale:references 将在您的 GuitarString 模型上创建一个
belongs_to :scale
,但您需要将 has_many :guitarstrings 添加到您的比例模型中。生成模型命令还会为您和所需的其他文件(固定装置)创建迁移脚本,类似于脚手架,但不会自动创建视图或控制器或任何内容。
编辑:
这通常是您想要做的事情 - 使用生成/模型或生成/视图或生成/控制器或生成/迁移。大多数 Rails 开发人员不使用脚手架,因为它的“一刀切”很少能完美地适应事物。然而,大多数 Rails 开发人员确实使用我提到的生成命令 - 它可以节省手动创建助手和装置的时间,并且它为它生成的每个文件提供一个可以添加到的基本模板。
一些 Ruby IDE(例如 JetBrain 的 RubyMine)具有基本上执行这些命令的宏。在 RubyMine 中,您可以执行 ctrl+alt+g,然后输入与您要生成的内容相对应的另一个键。
正如我所提到的,belongs_to 关系可以通过使用“引用”一词来生成。其他的您将手动添加。
I started learning Ruby on Rails a few weeks ago, and I've found it a lot easier to get the hang of things and learn my way around by not using scaffolding, and generating the various parts from the command line (or macros in an IDE).
However, from what I can tell, when you use scaffolding to generate things, you think of it as generating a "resource", so you're only going to create one resource at a time, then add in the relationships by hand later.
However, the generate model command can create these relationships for you. Lets say you used scaffolding to create a Scale resource.
You could then do
The scale:references will create a
belongs_to :scale
on your GuitarString model, but you'll need to add has_many :guitarstrings to your scale model.The generate model command also creates a migration script for you and other files needed (fixtures), similar to scaffolding, but doesn't autocreate views or controllers or anything.
EDIT:
This is generally how you are going to want to do things - use the generate/model or generate/view or generate/controller or generate/migration. Most Rails developers don't use scaffolding, since its "one size fits all" rarely fits things perfectly. However, most rails developers do use the generate commands I mentioned - it saves time with creating helpers and fixtures by hand, and it gives each file it generates a basic template you can add to.
Several Ruby IDE's like JetBrain's RubyMine have macros that essentially perform these commands. In RubyMine you can do ctrl+alt+g, then enter another key corresponding to what you want to generate.
The belongs_to relationship can be generated by using the "references" word, as I mentioned. Others you will add in by hand.