需要一些关于链接到 Rails 中的模型视图的帮助
我正在开发龙与地下城角色数据库应用程序。我有一个角色模型和几个属于具有 has_one 关联的角色的模型,例如 HitPoints、ArmorClass 等。这些都是从角色视图中构建、编辑和显示的。但是,我想创建包含属于角色模型的更多信息的新页面。我想要在视图顶部有一个链接,将用户带到设备页面,该页面上显示单独的模型,例如武器、盔甲、装备等。
这是我需要一些指导的地方。装备页面是否是属于角色的新模型,而我只需将武器、盔甲、装备等的模型加载到装备视图中?
如果是这样,这些模型是否与角色模型、设备模型或两者具有belongs_to关系?
最后,如果我这样构建:Character has_one Equipment和Equipment has_many Weapons,那么Weapons模型是否也与Character模型有belongs_to关系?
预先感谢,我希望我想要做的事情很清楚。我仍然无法谈论我想用 Rails 实现什么目标。
I'm working on a Dungeons and Dragons character database application. I've got a Character model and several models that belong to Character with a has_one association, such as HitPoints, ArmorClass and so on. These are all built, edited and displayed from the Character view. However, I want to create new pages with more information that belongs to the Character model. I want a link at the top of the view that takes the user to an Equipment page, with separate models displayed on that page, such as Weapons, Armor, Gear and so forth.
This is where I need some guidance. Would the Equipment page be a new model that belongs_to Character and I just load models for Weapon, Armor, Gear and so forth into the Equipment view?
If so, would these models have a belongs_to relationship with the Character model, the Equipment model or both?
Finally, if I build it this way: Character has_one Equipment and Equipment has_many Weapons, would the Weapons model also have a belongs_to relationship with the Character model?
Thanks in advance, I hope what I'm trying to do is clear. I'm still having trouble talking about what I want to accomplish with Rails.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您误解了模型的一些概念。例如,
HitPoints
和ArmorClass
不应该是与您的Character
具有has_one
关联的模型。它们只是Character
的属性。所以你的模型应该看起来像这样:然后你可以像这样访问它们:
另外,我想说
Equipment
模型是错误的概念。您需要的是一个Item
模型。你提到的所有东西(武器、盔甲、装备)都只是物品。而且“角色”has_many
“物品”更有意义。另外,“Item”has_many
“Characters”,因为您可能会拥有多个具有多个项目的字符......因此,为了保持简单,它可能是多对多的关系。然后你的“Item”模型可能有一个“ItemType”字段,它告诉你它是否是武器,盔甲,药水等。所以你的
Item
模型可能看起来像这样另一个选项,它更接近于您所说的是添加一个
ItemSet
模型。我们将此模型放在Character
和Items
之间。这样做会让你说has_one
“ItemSet”has_many
“Items”所以基本上,这表明在您的“世界”中您有许多角色和物品,但每个角色都有一组独特的物品,
方式。
这就是我在上面的代码中需要添加的 模型的
has_many
、has_one
等属性我省略了,这样您就可以更容易地看到模型本身,这样您就可以弄清楚您想要如何做到这一点。你自己。It sounds like you're misunderstanding some of the concepts around models. For example,
HitPoints
andArmorClass
should not be models that have ahas_one
association with yourCharacter
. They are just attributes ofCharacter
. So your model should look something like this instead:Then you can access them like so:
Also, I'd say
Equipment
model is the wrong concept. What you need is anItem
model. All of the things you mentioned (weapons, armor, gear) are all just items. And it makes a lot more sense that a "Character"has_many
"Items". Also that "Item"has_many
"Characters" since you'll presumably have multiple characters with multiple items...so it COULD be a many to many relationship to keep it simple. Then your "Item" model could have an "ItemType" field which tells you if it's a weapon, armor, potion, etc.So your
Item
model could look like thisAnother option, which is closer to what you're talking about is to add an
ItemSet
model. We'd put this model betweenCharacter
andItems
. Doing it this way would let you sayhas_one
"ItemSet"has_many
"Items"So basically, this is showing that in your "world" you have many characters and items. But each character has a unique set of those items.
This is the way I'd do it.
In the code above you need to add the
has_many
,has_one
, etc attributes to the models. I left it out so you could see the models themselves a little easier and so you could figure out how you want to do it yourself.