简体   繁体   English

在更复杂的Rails应用程序中如何考虑模型和关系?

[英]How to think about models and relationships in more complex Rails applications?

I've been learning Ruby on Rails for quite some time now and have built several toy applications. 我已经学习Ruby on Rails已有一段时间了,并且已经构建了多个玩具应用程序。 I've taken many classes/courses (ie, Hartl, Code School, Udemy, etc.). 我参加了许多课程(例如Hartl,Code School,Udemy等)。 Now I'm working on a pet project that is fairly complex - many models and relationships. 现在,我正在从事一个非常复杂的宠物项目-许多模型和关系。

Here's my question: How do I go about thinking about a complex application in terms of models and relationships? 这是我的问题:如何从模型和关系方面考虑复杂的应用程序? It seems to me there should be some visual way to model all of this, but I haven't seen any discussion of such modeling in any of the classes I've taken. 在我看来,应该有某种可视化的方式来对所有这些进行建模,但是在我所学习的任何类中,我都没有看到过关于这种建模的讨论。 Sure, there's the very simple diagrams in the Rails Guides, but how do I extend this to something more complex? 当然,Rails指南中有非常简单的图表,但是如何将其扩展到更复杂的东西呢? Or, am I making this too complicated? 还是我使这个变得太复杂了? Do I just start coding models and relationships and see where it takes me? 我是否只是开始对模型和关系进行编码,看看需要花些什么? It seems this ad hoc approach could easily paint me into a corner where I'll have to start over from scratch if I paint myself into a corner, so to speak. 看起来,这种临时方法很容易将我粉刷到一个角落,如果我将自己粉刷到一个角落,我将不得不从头开始。

Are there tools or blog posts that can help me? 有没有可以帮助我的工具或博客文章?

(Note: I've also posted this question at Reddit.Com/r/Rails at https://www.reddit.com/r/rails/comments/7c9zbf/how_to_think_about_rails_web_application/ ) (注意:我还在https://www.reddit.com/r/rails/comments/7c9zbf/how_to_think_about_rails_web_application/上的Reddit.Com/r/Rails上发布了此问题)

It sounds like what you're after is a schema designer. 听起来您想要的是架构设计器。 I'll link one at the bottom of this post. 我将在这篇文章的底部链接一个。 A schema designer will allow you to visualize all the relationships in your application, and see how one model connects to another. 模式设计器将允许您可视化应用程序中的所有关系,并查看一个模型如何连接到另一个模型。 They're really helpful in writing complex DB queries. 它们在编写复杂的数据库查询时确实很有帮助。 I've attached an example of a fairly simple design just to give you an idea what they do.You can also add all the columns a model has in the design, I just usually use it more for relationships. 我附上了一个相当简单的设计示例,目的是让您了解它们的功能。您还可以添加模型在设计中包含的所有列,我通常将其更多地用于关系。 http://ondras.zarovi.cz/sql/demo/ http://ondras.zarovi.cz/sql/demo/ 模式范例

Purely based on my experience/opinion... 纯粹根据我的经验/意见...

You'll end up changing your database design a couple of times as you build your app. 在构建应用程序时,您将不得不多次更改数据库设计。

I would start with a whiteboard, then consider using a schema designer as @aram mentioned. 我将从白板开始,然后考虑使用@aram提到的架构设计器。

After you see the big picture, just start with the relationships that what you need for today's features. 看到大局之后,就从建立当今功能所需的关系开始。 You can keep referring to the original design, so you can see the bigger picture, but you don't want to overbloat your architecture before you need to because it will change. 您可以继续参考原始设计,因此可以看到更大的图景,但是您不想在架构之前就夸大其词,因为它会发生变化。

After you write some code and build those relationships, you can spot check yourself using the rails-erd gem to programmatically generate your app's schema. 在编写了一些代码并建立了这些关系之后,您可以使用rails-erd gem进行现场检查,以编程方式生成应用程序的架构。

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

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